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 { fmt } from "@/pages/app/components/biz/BizUI";
import { bizInvestRequests } from "@/mocks/dshnBizDeals";

export default function BizInvestCounter() {
  const { goBack, params, navigate } = useApp();
  const req = bizInvestRequests.find((r) => r.id === params.requestId) ?? bizInvestRequests[0];

  const [amount, setAmount] = useState(String(req.amount));
  const [equity, setEquity] = useState(String(req.equity));
  const [sent, setSent] = useState(false);

  const valid = amount.trim() !== "" && equity.trim() !== "";

  if (sent) {
    return (
      <div className="min-h-full flex flex-col bg-background-50">
        <BackHeader title="عرض مقابل" onBack={goBack} />
        <div className="flex-1 flex flex-col items-center justify-center text-center px-6 py-14">
          <div className="w-20 h-20 rounded-full bg-accent-100 flex items-center justify-center mb-5">
            <i className="ri-mail-send-line text-[40px] text-accent-600 leading-none" />
          </div>
          <h2 className="text-[19px] font-bold text-foreground-950">تم إرسال العرض المقابل</h2>
          <p className="text-[13px] text-foreground-600 mt-2 max-w-[260px] leading-relaxed">
            بانتظار ردّ المستثمر على عرضك ({fmt(Number(amount) || 0)} ر.س مقابل {equity}%).
          </p>
          <div className="w-full max-w-[280px] mt-6 space-y-3">
            <Btn full onClick={() => navigate("bizInvestChat", { requestId: req.id })}>
              <i className="ri-chat-3-line text-[17px] leading-none" />
              فتح المحادثة
            </Btn>
            <Btn full variant="secondary" onClick={() => navigate("bizInvestRequests")}>
              طلبات الاستثمار
            </Btn>
          </div>
        </div>
      </div>
    );
  }

  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">
        <div className="rounded-[16px] bg-background-100 border border-background-200 p-3.5 flex items-center gap-3">
          <div className="w-11 h-11 rounded-full overflow-hidden bg-background-200 shrink-0">
            <img src={req.avatar} alt={req.investor} className="w-full h-full object-cover object-top" />
          </div>
          <div className="min-w-0">
            <p className="text-[13px] font-semibold text-foreground-950 truncate">{req.investor}</p>
            <p className="text-[11px] text-foreground-600 mt-0.5">
              عرضه الحالي: {fmt(req.amount)} ر.س مقابل {req.equity}%
            </p>
          </div>
        </div>

        <Field label="المبلغ الجديد (ر.س)" required>
          <TextInput value={amount} onChange={(v) => setAmount(v.replace(/\D/g, ""))} placeholder="0" dir="ltr" />
        </Field>

        <Field label="النسبة الجديدة (%)" required>
          <TextInput value={equity} onChange={(v) => setEquity(v.replace(/\D/g, "").slice(0, 2))} 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-[17px] text-foreground-500 leading-none mt-0.5" />
          <span className="text-[11.5px] 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={() => setSent(true)}>
          إرسال
        </Btn>
      </div>
    </div>
  );
}