import { useApp } from "@/pages/app/AppContext";
import { Badge, LogoMark, cn } from "@/pages/app/components/ui";
import { merchantStats, merchantProfile, merchantNotifications } from "@/mocks/dshnBusiness";
import { bizRecentActivity } from "@/mocks/dshnBizOwner";

type StatTone = "plain" | "accent" | "secondary";

const STATS: { icon: string; label: string; key: keyof typeof merchantStats; unit?: string; tone: StatTone }[] = [
  { icon: "ri-eye-line", label: "عدد المشاهدات", key: "views", tone: "plain" },
  { icon: "ri-shopping-bag-3-line", label: "عدد المنتجات", key: "products", tone: "plain" },
  { icon: "ri-file-list-3-line", label: "عدد الطلبات المدفوعة", key: "paidOrders", tone: "accent" },
  { icon: "ri-chat-check-line", label: "عدد طلبات التفاوض", key: "negotiationRequests", tone: "plain" },
  { icon: "ri-user-received-line", label: "عدد طلبات الاستثمار", key: "investmentRequests", tone: "secondary" },
  { icon: "ri-money-dollar-circle-line", label: "إجمالي الأرباح", key: "totalProfit", unit: "ر.س", tone: "accent" },
  { icon: "ri-team-line", label: "عدد الشراكات الحالية", key: "partnerships", tone: "plain" },
  { icon: "ri-line-chart-line", label: "إجمالي قيمة صفقات الاستثمار", key: "totalDealValue", unit: "ر.س", tone: "accent" },
];

const STAT_TONE: Record<StatTone, { card: string; icon: string }> = {
  plain: { card: "bg-background-100 border-background-200", icon: "bg-background-200 text-foreground-800" },
  accent: { card: "bg-accent-50 border-accent-200", icon: "bg-accent-100 text-accent-800" },
  secondary: { card: "bg-secondary-50 border-secondary-200", icon: "bg-secondary-100 text-secondary-900" },
};

export default function BizDashboard({ empty = false }: { empty?: boolean }) {
  const { navigate } = useApp();
  const unread = empty ? 0 : merchantNotifications.filter((n) => n.unread).length;
  const activity = empty ? [] : bizRecentActivity;
  const balance = empty ? 0 : merchantStats.availableBalance;

  return (
    <div className="min-h-full pb-6 bg-background-50">
      <h1 className="sr-only">الرئيسية</h1>

      {/* 1) Sticky top bar (60px) — logo + owner name + notifications bell only */}
      <header className="sticky top-0 z-30 h-[60px] bg-background-50/95 backdrop-blur border-b border-background-200/70 px-4 flex items-center gap-3">
        <div className="w-9 h-9 rounded-[11px] bg-primary-500 flex items-center justify-center shrink-0">
          <LogoMark className="text-[13px] text-background-50" />
        </div>
        <div className="flex-1 min-w-0">
          <p className="text-[10.5px] text-foreground-600 leading-none">صاحب بيزنس</p>
          <h2 className="text-[15px] font-bold text-foreground-950 truncate mt-1">{merchantProfile.name}</h2>
        </div>
        <button
          type="button"
          onClick={() => navigate("bizNotifications")}
          aria-label="الإشعارات"
          className="relative w-10 h-10 rounded-full flex items-center justify-center text-foreground-950 hover:bg-background-200 cursor-pointer shrink-0"
        >
          <i className="ri-notification-3-line text-[21px] leading-none" />
          {unread > 0 && (
            <span className="absolute -top-0.5 -left-0.5 min-w-[18px] h-[18px] px-1 rounded-full bg-primary-500 text-background-50 text-[10px] font-bold flex items-center justify-center">
              {unread}
            </span>
          )}
        </button>
      </header>

      <div className="px-4 pt-4 space-y-3.5">
        {/* 2) Prominent balance card (black, ~110px) */}
        <div className="relative h-[110px] rounded-[20px] bg-foreground-950 px-5 flex flex-col justify-center overflow-hidden">
          <div className="absolute -left-6 -top-6 w-28 h-28 rounded-full bg-background-50/5" />
          <div className="absolute -left-2 -bottom-10 w-24 h-24 rounded-full bg-background-50/5" />
          <div className="relative">
            <span className="text-[12px] text-background-50/70">الرصيد المتاح للسحب</span>
            <p className="text-[30px] font-bold text-background-50 mt-2 leading-none">
              {balance.toLocaleString("en-US")}
              <span className="text-[14px] font-semibold mr-1.5">ر.س</span>
            </p>
          </div>
        </div>

        {/* 3) Compact 2-column stat cards (~76px each) */}
        <div className="grid grid-cols-2 gap-[10px]">
          {STATS.map((s) => {
            const tone = STAT_TONE[s.tone];
            const value = empty ? 0 : merchantStats[s.key];
            return (
              <div
                key={s.key}
                className={cn("h-[76px] min-w-0 rounded-[16px] border px-2.5 flex items-center gap-2", tone.card)}
              >
                <span className={cn("w-8 h-8 rounded-[10px] flex items-center justify-center shrink-0", tone.icon)}>
                  <i className={cn(s.icon, "text-[16px] leading-none")} />
                </span>
                <div className="min-w-0 flex-1">
                  <p className="text-[15px] font-bold text-foreground-950 leading-tight whitespace-nowrap">
                    {value.toLocaleString("en-US")}
                    {s.unit && <span className="text-[10.5px] font-semibold mr-1">{s.unit}</span>}
                  </p>
                  <p className="text-[12px] text-foreground-600 mt-1 leading-tight">{s.label}</p>
                </div>
              </div>
            );
          })}
        </div>

        {/* 4) Latest orders / negotiation requests */}
        <section>
          <h2 className="text-[15px] font-semibold text-foreground-950 mb-2.5">آخر الطلبات</h2>
          {activity.length === 0 ? (
            <div className="rounded-[16px] bg-background-100 border border-background-200 py-8 flex flex-col items-center text-center">
              <div className="w-12 h-12 rounded-full bg-background-200 flex items-center justify-center mb-3">
                <i className="ri-inbox-line text-[22px] text-foreground-500 leading-none" />
              </div>
              <p className="text-[13px] font-semibold text-foreground-950">لا توجد طلبات بعد</p>
              <p className="text-[11.5px] text-foreground-600 mt-1">ستظهر هنا أحدث الطلبات وطلبات التفاوض.</p>
            </div>
          ) : (
            <div className="space-y-2.5">
              {activity.map((a) => (
                <div
                  key={a.id}
                  className="rounded-[16px] bg-background-100 border border-background-200 p-3.5 flex items-center gap-3"
                >
                  <div
                    className={cn(
                      "w-10 h-10 rounded-[12px] flex items-center justify-center shrink-0",
                      a.kind === "order"
                        ? "bg-accent-100 text-accent-700"
                        : "bg-secondary-100 text-secondary-800",
                    )}
                  >
                    <i
                      className={cn(
                        a.kind === "order" ? "ri-shopping-bag-3-line" : "ri-chat-check-line",
                        "text-[18px] leading-none",
                      )}
                    />
                  </div>
                  <div className="flex-1 min-w-0">
                    <p className="text-[13px] font-semibold text-foreground-950 truncate">{a.title}</p>
                    <p className="text-[11.5px] text-foreground-600 mt-1 truncate">{a.subtitle}</p>
                  </div>
                  <Badge tone={a.statusTone} className="shrink-0">
                    {a.statusLabel}
                  </Badge>
                </div>
              ))}
            </div>
          )}
        </section>
      </div>
    </div>
  );
}