import { useApp } from "@/pages/app/AppContext";
import { BackHeader, Badge } from "@/pages/app/components/ui";
import { fmt } from "@/pages/app/components/biz/BizUI";
import { earningsRows } from "@/mocks/dshnBusiness";
import { earningsSummary, partnershipReceivables } 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>
  );
}

/** Earnings & withdrawal — earnings tab (section 2.23). */
export default function BizEarnings() {
  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={fmt(earningsSummary.totalProfit)} unit="ر.س" />
          <StatTile label="الرصيد المتاح للسحب" value={fmt(earningsSummary.availableBalance)} unit="ر.س" />
          <StatTile label="مجموع الطلبات المكتملة" value={fmt(earningsSummary.completedOrders)} />
        </div>

        {/* Withdraw button (black) */}
        <button
          type="button"
          onClick={() => navigate("bizWithdraw")}
          className="w-full h-[52px] rounded-[14px] bg-foreground-950 text-background-50 text-[15px] font-semibold flex items-center justify-center gap-2 whitespace-nowrap cursor-pointer hover:bg-foreground-900 transition-colors"
        >
          <i className="ri-hand-coin-line text-[19px] leading-none" />
          طلب سحب
        </button>

        {/* 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("bizWithdrawals")}
            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>

        {/* Section 1 — أرباح الطلبات */}
        <section>
          <h2 className="text-[15px] font-semibold text-foreground-950 mb-2.5">أرباح الطلبات</h2>
          <div className="space-y-2.5">
            {earningsRows.map((e) => (
              <div key={e.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">
                    طلب #{e.orderNumber} · {e.customer}
                  </p>
                  <Badge tone={e.statusTone} className="shrink-0">
                    {e.status}
                  </Badge>
                </div>
                <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">{fmt(e.productsValue)} ر.س</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">{fmt(e.commission)} ر.س</span>
                  </div>
                  {e.discountShare > 0 && (
                    <div className="flex items-center justify-between text-[11.5px]">
                      <span className="text-foreground-600">حصة كود الخصم</span>
                      <span className="font-medium text-foreground-900">{fmt(e.discountShare)} ر.س</span>
                    </div>
                  )}
                  <div className="flex items-center justify-between text-[12.5px] pt-1 border-t border-background-200">
                    <span className="text-foreground-700 font-medium">صافي الربح</span>
                    <span className="font-bold text-foreground-950">{fmt(e.net)} ر.س</span>
                  </div>
                </div>
                <p className="text-[10.5px] text-foreground-500 mt-2">{e.date}</p>
              </div>
            ))}
          </div>
        </section>

        {/* Section 2 — مستحقات الشراكات */}
        <section>
          <h2 className="text-[15px] font-semibold text-foreground-950 mb-2.5">مستحقات الشراكات</h2>
          <div className="space-y-2.5">
            {partnershipReceivables.map((p) => (
              <div key={p.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">
                    شراكة #{p.partnershipNumber}
                  </p>
                  <Badge tone={p.statusTone} className="shrink-0">
                    {p.statusLabel}
                  </Badge>
                </div>
                <div className="flex items-center justify-between text-[11.5px] mt-2">
                  <span className="text-foreground-600">{p.payerName}</span>
                  <span className="font-bold text-foreground-950">{fmt(p.value)} ر.س</span>
                </div>
                <p className="text-[10.5px] text-foreground-500 mt-1">{p.date}</p>
              </div>
            ))}
          </div>

          {/* Note */}
          <div className="flex items-start gap-2 rounded-[14px] bg-background-100 border border-background-200 px-3.5 py-3 mt-2.5">
            <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>
        </section>
      </div>
    </div>
  );
}