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

/** Prepare and send the partnership invoice to the other party (section 2.19). */
export default function BizPartnerInvoice() {
  const { goBack, navigate, params } = useApp();
  const { partnershipById, setPartnershipStatus } = useProductStore();
  const id = String(params.partnerId ?? "pt1");
  const row = partnershipById(id);
  const [value, setValue] = useState(row && row.value > 0 ? String(row.value) : "45000");

  const valid = Number(value || 0) > 0;
  const amount = Number(value || 0);

  const submit = () => {
    setPartnershipStatus(id, "awaitingApproval", {
      value: amount,
      note: `تم إرسال فاتورة الشراكة بقيمة ${amount.toLocaleString("en-US")} ر.س — بانتظار موافقة الطرف الآخر`,
    });
    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">
          <div className="flex items-center gap-3">
            <span className="w-9 h-9 rounded-[11px] bg-background-200 flex items-center justify-center shrink-0">
              <i className="ri-store-3-line text-[17px] text-foreground-800 leading-none" />
            </span>
            <div className="min-w-0">
              <p className="text-[11px] text-foreground-600">منشأتي</p>
              <p className="text-[13.5px] font-semibold text-foreground-950 truncate">
                {bizPartnershipSides.mine.name}
              </p>
              <p className="text-[11px] text-foreground-600 mt-0.5">
                {bizPartnershipSides.mine.field} · {bizPartnershipSides.mine.city}
              </p>
            </div>
          </div>
          <div className="h-px bg-background-200" />
          <div className="flex items-center gap-3">
            <span className="w-9 h-9 rounded-[11px] bg-background-200 flex items-center justify-center shrink-0">
              <i className="ri-building-2-line text-[17px] text-foreground-800 leading-none" />
            </span>
            <div className="min-w-0">
              <p className="text-[11px] text-foreground-600">الطرف الآخر</p>
              <p className="text-[13.5px] font-semibold text-foreground-950 truncate">
                {row?.partnerName ?? bizPartnershipSides.partner.name}
              </p>
              <p className="text-[11px] text-foreground-600 mt-0.5">
                {bizPartnershipSides.partner.field} · {row?.partnerCity ?? bizPartnershipSides.partner.city}
              </p>
            </div>
          </div>
        </div>

        <Field label="قيمة الشراكة (ر.س)" required>
          <TextInput value={value} onChange={(v) => setValue(v.replace(/\D/g, ""))} placeholder="0" dir="ltr" />
        </Field>

        <div className="flex items-start gap-2 rounded-[14px] bg-background-100 border border-background-200 px-3.5 py-3">
          <i className="ri-information-line text-[16px] text-foreground-500 leading-none mt-0.5" />
          <span className="text-[11px] text-foreground-600 leading-relaxed">
            بعد إرسال الفاتورة تتحول حالة الشراكة إلى «بانتظار الموافقة» من الطرف الآخر.
          </span>
        </div>
      </div>

      <div className="sticky bottom-0 z-30 px-4 py-4 bg-background-50 border-t border-background-200/70">
        <Btn full disabled={!valid} onClick={submit}>
          إرسال الفاتورة للطرف الآخر
        </Btn>
      </div>
    </div>
  );
}