import { useMemo, useState } from "react";
import { useApp } from "@/pages/app/AppContext";
import { BackHeader, Badge, EmptyState } from "@/pages/app/components/ui";
import { fmt } from "@/pages/app/components/biz/BizUI";
import { BizStatusFilterRow, type BizStatusChip } from "@/pages/app/components/biz/BizStatusFilterRow";
import {
  bizInvestRequests,
  bizDealStatusLabel,
  bizDealStatusTone,
  type BizDealStatus,
} from "@/mocks/dshnBizDeals";

type FilterKey = BizDealStatus | "all";

const FILTERS: { key: FilterKey; label: string }[] = [
  { key: "all", label: "الكل" },
  { key: "new", label: "طلب جديد" },
  { key: "talking", label: "جاري التواصل" },
  { key: "negotiating", label: "تفاوض" },
  { key: "agreed", label: "تم الاتفاق" },
  { key: "awaitValue", label: "بانتظار تسجيل القيمة" },
  { key: "registered", label: "تم تسجيل الصفقة" },
  { key: "commission", label: "العمولة مستحقة" },
  { key: "done", label: "مكتملة" },
  { key: "cancelled", label: "ملغاة" },
  { key: "rejected", label: "مرفوضة" },
];

export default function BizInvestRequests() {
  const { goBack, navigate } = useApp();
  const [filter, setFilter] = useState<FilterKey>("all");

  const chips: BizStatusChip[] = useMemo(
    () =>
      FILTERS.map((f) => ({
        key: f.key,
        label: f.label,
        count:
          f.key === "all"
            ? bizInvestRequests.length
            : bizInvestRequests.filter((r) => r.status === f.key).length,
      })),
    [],
  );

  const list = useMemo(
    () =>
      filter === "all"
        ? bizInvestRequests
        : bizInvestRequests.filter((r) => r.status === filter),
    [filter],
  );

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

      <BizStatusFilterRow chips={chips} active={filter} onChange={(k) => setFilter(k as FilterKey)} />

      <div className="px-4 pt-2 pb-5 space-y-3">
        {list.length === 0 ? (
          <EmptyState icon="ri-inbox-line" title="لا توجد طلبات في هذه الحالة" />
        ) : (
          list.map((r) => (
            <button
              key={r.id}
              type="button"
              onClick={() => navigate("bizInvestRequest", { requestId: r.id })}
              className="w-full text-right rounded-[18px] bg-background-100 border border-background-200 p-3.5 cursor-pointer hover:border-background-300 transition-colors"
            >
              <div className="flex items-center gap-3">
                <div className="w-12 h-12 rounded-full overflow-hidden bg-background-200 shrink-0">
                  <img src={r.avatar} alt={r.investor} className="w-full h-full object-cover object-top" />
                </div>
                <div className="flex-1 min-w-0">
                  <div className="flex items-center justify-between gap-2">
                    <h3 className="text-[14px] font-semibold text-foreground-950 truncate">{r.investor}</h3>
                    <Badge tone={bizDealStatusTone[r.status]}>{bizDealStatusLabel[r.status]}</Badge>
                  </div>
                  <p className="text-[11px] text-foreground-600 mt-0.5">
                    {r.city} • {r.date}
                  </p>
                </div>
              </div>

              <div className="grid grid-cols-2 gap-2 mt-3">
                <div className="rounded-[12px] bg-background-200/70 p-2.5">
                  <p className="text-[10px] text-foreground-600">المبلغ المقترح</p>
                  <p className="text-[13px] font-bold text-foreground-950 mt-0.5">{fmt(r.amount)} ر.س</p>
                </div>
                <div className="rounded-[12px] bg-accent-100 p-2.5">
                  <p className="text-[10px] text-accent-800">النسبة المطلوبة</p>
                  <p className="text-[13px] font-bold text-accent-900 mt-0.5">{r.equity}%</p>
                </div>
              </div>
            </button>
          ))
        )}
      </div>
    </div>
  );
}