import { useApp } from "@/pages/app/AppContext";
import { BackHeader, Badge, cn } from "@/pages/app/components/ui";
import {
  commissionRows,
  commissionStatusLabel,
  commissionStatusTone,
  commissionsSummary,
} from "@/mocks/dshnBizPartners";

function StatTile({ label, value, unit }: { label: string; value: string; unit?: string }) {
  return (
    <div className="min-w-0 rounded-[16px] bg-background-100 border border-background-200 p-3">
      <p className="text-[10px] text-foreground-600 leading-snug h-[26px]">{label}</p>
      <p className="text-[15px] font-bold text-foreground-950 mt-1 leading-tight truncate">
        {value}
        {unit && <span className="text-[10px] font-semibold mr-1">{unit}</span>}
      </p>
    </div>
  );
}

/** Investment commission settlement — transaction log tab (section 2.24). */
export default function BizCommissions() {
  const { goBack, navigate } = useApp();

  return (
    <div className="min-h-full pb-6 bg-background-50">
      <BackHeader title="تسوية عمولات الاستثمار" onBack={goBack} />

      <div className="px-4 py-5 space-y-4">
        {/* Stat cards — 3 equal */}
        <div className="grid grid-cols-3 gap-2">
          <StatTile label="إجمالي صفقات الاستثمار" value={commissionsSummary.totalDeals.toLocaleString("en-US")} />
          <StatTile
            label="إجمالي العمولات"
            value={commissionsSummary.totalCommissions.toLocaleString("en-US")}
            unit="ر.س"
          />
          <StatTile
            label="الرصيد المستحق"
            value={commissionsSummary.dueBalance.toLocaleString("en-US")}
            unit="ر.س"
          />
        </div>

        {/* Tabs */}
        <div className="flex gap-1 bg-background-100 border border-background-200 rounded-full p-1">
          <span className="flex-1 h-11 rounded-full text-[12.5px] font-medium flex items-center justify-center whitespace-nowrap bg-primary-500 text-background-50">
            سجل العمليات
          </span>
          <button
            type="button"
            onClick={() => navigate("bizSettlement")}
            className="flex-1 h-11 rounded-full text-[12.5px] font-medium whitespace-nowrap cursor-pointer text-foreground-700 hover:bg-background-200 transition-colors"
          >
            تسويات العمولة
          </button>
        </div>

        {/* Transactions */}
        <div className="space-y-2.5">
          {commissionRows.map((d) => (
            <div key={d.id} className="rounded-[16px] bg-background-100 border border-background-200 p-3.5">
              <div className="flex items-center justify-between gap-2">
                <p className="text-[13px] font-semibold text-foreground-950 truncate">{d.dealNumber}</p>
                <Badge tone={commissionStatusTone[d.status]} className="shrink-0">
                  {commissionStatusLabel[d.status]}
                </Badge>
              </div>
              <p className="text-[11.5px] text-foreground-600 mt-1.5 truncate">
                {d.investor} · {d.opportunity}
              </p>
              <div className="mt-2.5 space-y-1">
                <div className="flex items-center justify-between text-[11.5px]">
                  <span className="text-foreground-600">قيمة الصفقة</span>
                  <span className="font-medium text-foreground-900">{d.value.toLocaleString("en-US")} ر.س</span>
                </div>
                <div className="flex items-center justify-between text-[11.5px]">
                  <span className="text-foreground-600">نسبة العمولة</span>
                  <span className="font-medium text-foreground-900">{d.rate}%</span>
                </div>
                <div className="flex items-center justify-between text-[11.5px]">
                  <span className="text-foreground-600">قيمة العمولة</span>
                  <span className="font-bold text-foreground-950">{d.commission.toLocaleString("en-US")} ر.س</span>
                </div>
              </div>
              <p className="text-[10.5px] text-foreground-500 mt-2">{d.date}</p>
            </div>
          ))}
        </div>

        {/* Gray note */}
        <div className={cn("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>
  );
}