import { useState } from "react";
import { useApp } from "@/pages/app/AppContext";
import { BackHeader, Badge, cn } from "@/pages/app/components/ui";
import { Sheet } from "@/pages/app/components/bo/BoUI";
import { fmt } from "@/pages/app/components/biz/BizUI";
import { withdrawals, type Withdrawal } from "@/mocks/dshnBusiness";

type SubId = "review" | "confirmed" | "rejected";

const SUBS: { id: SubId; label: string }[] = [
  { id: "review", label: "قيد المراجعة" },
  { id: "confirmed", label: "مؤكدة" },
  { id: "rejected", label: "مرفوضة" },
];

const TONE: Record<SubId, "warn" | "success" | "danger"> = {
  review: "warn",
  confirmed: "success",
  rejected: "danger",
};

/** Withdrawals tab with sub-tabs (section 2.23). */
export default function BizWithdrawals() {
  const { goBack, navigate } = useApp();
  const [sub, setSub] = useState<SubId>("review");
  const [proof, setProof] = useState<Withdrawal | null>(null);

  const rows = withdrawals.filter((w) => w.status === sub);

  return (
    <div className="min-h-full pb-6 bg-background-50">
      <BackHeader title="طلبات السحب" onBack={goBack} />

      <div className="px-4 py-5 space-y-4">
        {/* Tabs */}
        <div className="flex gap-1 bg-background-100 border border-background-200 rounded-full p-1">
          <button
            type="button"
            onClick={() => navigate("bizEarnings")}
            className="flex-1 h-11 rounded-full text-[12.5px] font-medium whitespace-nowrap cursor-pointer text-foreground-700 hover:bg-background-200 transition-colors"
          >
            الأرباح
          </button>
          <span className="flex-1 h-11 rounded-full text-[12.5px] font-medium flex items-center justify-center whitespace-nowrap bg-primary-500 text-background-50">
            طلبات السحب
          </span>
        </div>

        {/* Sub-tabs (nowrap, swipeable row) */}
        <div className="flex gap-2 overflow-x-auto no-scrollbar pb-0.5">
          {SUBS.map((s) => (
            <button
              key={s.id}
              type="button"
              onClick={() => setSub(s.id)}
              className={cn(
                "shrink-0 rounded-full px-4 h-10 text-[12.5px] font-medium whitespace-nowrap transition-colors cursor-pointer",
                sub === s.id
                  ? "bg-primary-500 text-background-50"
                  : "bg-background-100 border border-background-200 text-foreground-700",
              )}
            >
              {s.label}
            </button>
          ))}
        </div>

        {/* Cards */}
        {rows.length === 0 ? (
          <div className="py-16 flex flex-col items-center text-center">
            <div className="w-16 h-16 rounded-full bg-background-100 border border-background-200 flex items-center justify-center mb-4">
              <i className="ri-inbox-line text-[28px] text-foreground-400 leading-none" />
            </div>
            <p className="text-[14px] font-medium text-foreground-700">لا توجد طلبات سحب</p>
          </div>
        ) : (
          <div className="space-y-2.5">
            {rows.map((w) => (
              <div key={w.id} className="rounded-[16px] bg-background-100 border border-background-200 p-3.5">
                <div className="flex items-center justify-between gap-2">
                  <p className="text-[14px] font-bold text-foreground-950">{fmt(w.amount)} ر.س</p>
                  <Badge tone={TONE[w.status]} className="shrink-0">
                    {SUBS.find((s) => s.id === w.status)?.label}
                  </Badge>
                </div>
                <p className="text-[11px] text-foreground-600 mt-1">{w.date}</p>

                {w.status === "confirmed" && w.transferImage && (
                  <button
                    type="button"
                    onClick={() => setProof(w)}
                    className="mt-2.5 flex items-center gap-2 cursor-pointer"
                  >
                    <span className="w-14 h-14 rounded-[12px] overflow-hidden bg-background-200 border border-background-200 shrink-0">
                      <img src={w.transferImage} alt="إشعار التحويل" className="w-full h-full object-cover object-top" />
                    </span>
                    <span className="text-[11.5px] font-medium text-primary-700 flex items-center gap-1">
                      <i className="ri-eye-line text-[15px] leading-none" />
                      عرض إشعار التحويل
                    </span>
                  </button>
                )}

                {w.status === "rejected" && w.reason && (
                  <div className="mt-2.5 flex items-start gap-2 rounded-[12px] bg-danger-50 border border-danger-200 px-3 py-2.5">
                    <i className="ri-error-warning-line text-[15px] text-danger-600 leading-none mt-0.5" />
                    <p className="text-[11px] text-danger-700 leading-relaxed">{w.reason}</p>
                  </div>
                )}
              </div>
            ))}
          </div>
        )}
      </div>

      {/* Transfer image viewer */}
      <Sheet open={proof !== null} onClose={() => setProof(null)}>
        <h3 className="text-[15px] font-bold text-foreground-950 mb-3">إشعار التحويل</h3>
        <div className="w-full h-[220px] rounded-[16px] overflow-hidden bg-background-200">
          {proof?.transferImage && (
            <img src={proof.transferImage} alt="إشعار التحويل" className="w-full h-full object-cover object-top" />
          )}
        </div>
        <p className="text-[12px] text-foreground-600 mt-3 leading-relaxed">
          تم تحويل مبلغ {proof ? fmt(proof.amount) : ""} ر.س إلى حسابك البنكي المسجل.
        </p>
      </Sheet>
    </div>
  );
}