import { useState } from "react";
import { useApp } from "@/pages/app/AppContext";
import { BackHeader, Btn, cn } from "@/pages/app/components/ui";
import { TextArea } from "@/pages/app/components/bo/BoUI";
import { bizPartnerTermsList } from "@/mocks/dshnBizPartners";
import { useProductStore } from "@/pages/product/store";

/** Other party reviews the partnership contract: approve / reject with reason (section 2.20). */
export default function BizPartnerReview() {
  const { goBack, navigate, params } = useApp();
  const { partnershipById, approvePartnership, rejectPartnership } = useProductStore();
  const id = String(params.partnerId ?? "pt1");
  const row = partnershipById(id);
  const [reason, setReason] = useState("");
  const [rejectOpen, setRejectOpen] = useState(false);

  const value = row && row.value > 0 ? row.value : 45000;
  const partnerName = row?.partnerName ?? "دار الأزياء";
  const partnerCity = row?.partnerCity ?? "الرياض";

  const approve = () => {
    approvePartnership(id);
    navigate("bizPartnership", { partnerId: id, flash: "1" });
  };

  const confirmReject = () => {
    rejectPartnership(id, reason);
    setRejectOpen(false);
    navigate("bizPartnership", { partnerId: id, flash: "1" });
  };

  return (
    <div className="min-h-full flex flex-col bg-background-50">
      <BackHeader title="مراجعة عقد الشراكة" onBack={goBack} />

      <div className="flex-1 px-4 py-5 space-y-4">
        {/* Both businesses */}
        <div className="rounded-[20px] bg-background-100 border border-background-200 p-4 space-y-3">
          {[
            { name: "متجر النخبة", meta: "أغذية ومشروبات · الرياض" },
            { name: partnerName, meta: `أزياء · ${partnerCity}` },
          ].map((b) => (
            <div key={b.name} className="flex items-center justify-between gap-3">
              <div className="min-w-0">
                <p className="text-[13px] font-semibold text-foreground-950 truncate">{b.name}</p>
                <p className="text-[11px] text-foreground-600 mt-0.5">{b.meta}</p>
              </div>
            </div>
          ))}
          <div className="h-px bg-background-200" />
          <div className="flex items-center justify-between">
            <span className="text-[12.5px] text-foreground-600">قيمة الشراكة</span>
            <span className="text-[14px] font-bold text-foreground-950">
              {value.toLocaleString("en-US")} ر.س
            </span>
          </div>
        </div>

        {/* Contract terms */}
        <div className="rounded-[20px] bg-background-100 border border-background-200 p-4">
          <h2 className="text-[14px] font-semibold text-foreground-950 mb-3">بنود عقد الشراكة</h2>
          <ol className="space-y-3">
            {bizPartnerTermsList.map((term, i) => (
              <li key={i} className="flex items-start gap-2.5">
                <span className="w-6 h-6 rounded-full bg-background-200 text-foreground-800 text-[11px] font-bold flex items-center justify-center shrink-0">
                  {i + 1}
                </span>
                <p className="text-[12px] text-foreground-700 leading-relaxed">{term}</p>
              </li>
            ))}
          </ol>
        </div>
      </div>

      <div className="sticky bottom-0 z-30 px-4 py-4 bg-background-50 border-t border-background-200/70">
        <div className="flex gap-3">
          <div className="flex-1">
            <Btn full onClick={approve}>
              موافقة
            </Btn>
          </div>
          <div className="flex-1">
            <Btn variant="danger" full onClick={() => setRejectOpen(true)}>
              رفض
            </Btn>
          </div>
        </div>
      </div>

      {/* Reject reason popup */}
      {rejectOpen && (
        <div className="fixed inset-0 z-[60] flex items-center justify-center px-6">
          <div onClick={() => setRejectOpen(false)} className="absolute inset-0 bg-foreground-950/45" />
          <div className="relative w-full max-w-[340px] rounded-[22px] bg-background-50 p-5 animate-fade-in">
            <div className="flex flex-col items-center text-center">
              <div className="w-14 h-14 rounded-full bg-danger-100 flex items-center justify-center mb-3">
                <i className="ri-close-circle-line text-[30px] text-danger-500 leading-none" />
              </div>
              <h3 className="text-[16px] font-bold text-foreground-950">رفض عقد الشراكة</h3>
              <p className="text-[12.5px] text-foreground-600 mt-1.5 leading-relaxed">
                اكتب سبب الرفض عشان يوضح للطرف التاني قبل التأكيد.
              </p>
            </div>

            <div className="mt-4">
              <p className="text-[12.5px] font-semibold text-danger-700 mb-2">سبب الرفض</p>
              <TextArea
                value={reason}
                onChange={setReason}
                placeholder="اكتب سبب رفض عقد الشراكة..."
                rows={3}
              />
            </div>

            <div className="mt-4 space-y-2.5">
              <button
                type="button"
                onClick={confirmReject}
                disabled={reason.trim() === ""}
                className={cn(
                  "w-full h-[48px] rounded-[13px] text-[14px] font-semibold text-background-50 bg-danger-500 hover:bg-danger-600 cursor-pointer transition-colors",
                  reason.trim() === "" && "opacity-40 pointer-events-none",
                )}
              >
                تأكيد الرفض
              </button>
              <button
                type="button"
                onClick={() => setRejectOpen(false)}
                className="w-full h-[48px] rounded-[13px] text-[14px] font-semibold text-foreground-900 border border-background-300 hover:bg-background-200 cursor-pointer"
              >
                إلغاء
              </button>
            </div>
          </div>
        </div>
      )}
    </div>
  );
}