import { useState } from "react";
import { investorCommitment } from "@/mocks/dshnInvestor";
import { useWeb } from "@/pages/web/WebContext";
import { Container, WCard, WBtn, cn, fmt } from "@/pages/web/WebUI";

export default function InvPayment() {
  const { navigate } = useWeb();
  const c = investorCommitment;
  const [proof, setProof] = useState<string | null>(null);
  const [copied, setCopied] = useState(false);
  const [sent, setSent] = useState(false);

  const copyIban = async () => {
    try {
      await navigator.clipboard.writeText(c.iban.replace(/\s/g, ""));
    } catch {
      /* clipboard may be unavailable */
    }
    setCopied(true);
    setTimeout(() => setCopied(false), 1600);
  };

  if (sent) {
    return (
      <Container className="py-14">
        <div className="max-w-[560px] mx-auto animate-fade-up">
          <WCard className="p-8 text-center">
            <span className="w-20 h-20 rounded-full bg-accent-100 text-accent-800 flex items-center justify-center mx-auto mb-6">
              <i className="ri-checkbox-circle-line text-[42px] leading-none" />
            </span>
            <h1 className="text-[23px] font-bold text-foreground-950 font-heading">تم إرسال إثبات التحويل</h1>
            <p className="text-[14px] text-foreground-600 mt-3 leading-relaxed">
              سيتم مراجعة الإثبات واعتماده خلال 24 ساعة، وستصلك الإشعارات بذلك.
            </p>
            <div className="space-y-3 mt-6">
              <WBtn full onClick={() => navigate("invFollowing")}>
                الذهاب إلى المتابعة
              </WBtn>
              <WBtn full variant="secondary" onClick={() => navigate("invHome")}>
                الرئيسية
              </WBtn>
            </div>
          </WCard>
        </div>
      </Container>
    );
  }

  return (
    <div className="animate-fade-in bg-background-50">
      <Container className="py-12">
        <div className="max-w-[720px] mx-auto">
          <div className="mb-8">
            <h1 className="text-[27px] md:text-[32px] font-bold text-foreground-950 font-heading">دفع مبلغ الالتزام</h1>
            <p className="text-[14px] text-foreground-600 mt-2.5 leading-relaxed">
              قم بتحويل مبلغ الالتزام إلى الحساب التالي ثم ارفع إثبات التحويل.
            </p>
          </div>

          <WCard className="p-6 flex items-center gap-4">
            <div className="w-[52px] h-[52px] rounded-full overflow-hidden bg-background-200 shrink-0">
              <img src={c.businessLogo} alt={c.businessName} className="w-full h-full object-cover object-top" />
            </div>
            <div className="flex-1 min-w-0">
              <h3 className="text-[15px] font-semibold text-foreground-950 truncate">{c.businessName}</h3>
              <p className="text-[12px] text-foreground-600 mt-0.5">التزام استثمار تشاركي</p>
            </div>
            <div className="text-end">
              <p className="text-[11.5px] text-foreground-600">المبلغ المستحق</p>
              <p className="text-[19px] font-bold text-foreground-950">{fmt(c.amountDue)} ر.س</p>
            </div>
          </WCard>

          <WCard className="p-7 mt-6">
            <h2 className="text-[16px] font-bold text-foreground-950 mb-4">بيانات التحويل</h2>
            <div className="space-y-3">
              <DetailRow label="اسم البنك" value={c.bankName} />
              <DetailRow label="اسم الحساب" value={c.accountName} />
              <div className="flex items-center justify-between gap-3 rounded-[14px] bg-background-50 border border-background-200 p-4">
                <div className="min-w-0">
                  <p className="text-[11.5px] text-foreground-600">رقم الآيبان (IBAN)</p>
                  <p dir="ltr" className="text-[14px] font-bold text-foreground-950 mt-1 break-all">{c.iban}</p>
                </div>
                <button
                  type="button"
                  onClick={copyIban}
                  className="h-10 px-4 rounded-[12px] bg-background-200 text-foreground-800 text-[12.5px] font-semibold flex items-center gap-1.5 cursor-pointer hover:bg-background-300 shrink-0 whitespace-nowrap"
                >
                  <i className={cn(copied ? "ri-check-line" : "ri-file-copy-line", "text-[16px] leading-none")} />
                  {copied ? "تم النسخ" : "نسخ"}
                </button>
              </div>
              <div className="flex items-center gap-2 rounded-[14px] bg-secondary-50 border border-secondary-200 px-4 py-3.5">
                <i className="ri-time-line text-[18px] text-secondary-800 leading-none" />
                <span className="text-[13px] text-secondary-900">آخر موعد للتحويل: {c.deadline}</span>
              </div>
            </div>
          </WCard>

          <WCard className="p-7 mt-6">
            <h2 className="text-[16px] font-bold text-foreground-950 mb-4">إثبات التحويل</h2>
            <label className="block cursor-pointer">
              <input
                type="file"
                accept="image/*,.pdf"
                className="hidden"
                onChange={(e) => setProof(e.target.files?.[0]?.name ?? null)}
              />
              <div className="rounded-[16px] border border-dashed border-background-300 bg-background-50 p-8 text-center hover:border-foreground-400 transition-colors">
                {proof ? (
                  <div className="flex items-center justify-center gap-2 text-[13.5px] text-foreground-800">
                    <i className="ri-file-pdf-2-line text-[20px] text-danger-500 leading-none" />
                    {proof}
                  </div>
                ) : (
                  <>
                    <span className="w-14 h-14 rounded-full bg-background-200 flex items-center justify-center mx-auto mb-3">
                      <i className="ri-upload-cloud-2-line text-[26px] text-foreground-600 leading-none" />
                    </span>
                    <p className="text-[13.5px] font-semibold text-foreground-950">ارفع صورة أو ملف PDF</p>
                    <p className="text-[12px] text-foreground-500 mt-1">بحد أقصى 10 ميجابايت</p>
                  </>
                )}
              </div>
            </label>
          </WCard>

          <div className="flex flex-col sm:flex-row gap-3 mt-6">
            <WBtn className="flex-1" size="lg" disabled={!proof} onClick={() => setSent(true)}>
              إرسال الإثبات
            </WBtn>
            <WBtn className="flex-1" size="lg" variant="secondary" onClick={() => navigate("invFollowing")}>
              لاحقًا
            </WBtn>
          </div>
        </div>
      </Container>
    </div>
  );
}

function DetailRow({ label, value }: { label: string; value: string }) {
  return (
    <div className="flex items-center justify-between gap-3 rounded-[14px] bg-background-50 border border-background-200 p-4">
      <span className="text-[12.5px] text-foreground-600">{label}</span>
      <span className="text-[14px] font-bold text-foreground-950 text-end">{value}</span>
    </div>
  );
}