import { useState } from "react";
import { useApp } from "../AppContext";
import { businesses } from "@/mocks/dshn";
import { orders } from "@/mocks/dshnCommerce";
import { BackHeader, Badge, cn } from "../components/ui";

type Tab = "new" | "current" | "done";

const TAB_LABELS: { id: Tab; label: string }[] = [
  { id: "new", label: "جديدة" },
  { id: "current", label: "حالية" },
  { id: "done", label: "منتهية" },
];

export default function Orders() {
  const { goBack, navigate } = useApp();
  const [tab, setTab] = useState<Tab>("new");

  const count = (t: Tab) => orders.filter((o) => o.status === t).length;
  const list = orders.filter((o) => o.status === tab);

  return (
    <div className="min-h-full pb-6">
      <BackHeader title="طلباتي" onBack={goBack} />

      <div className="px-4 mt-4">
        <div className="flex bg-background-200 rounded-full p-1">
          {TAB_LABELS.map((t) => {
            const active = tab === t.id;
            return (
              <button
                key={t.id}
                type="button"
                onClick={() => setTab(t.id)}
                className={cn(
                  "flex-1 h-10 rounded-full text-[13px] font-semibold flex items-center justify-center gap-1.5 cursor-pointer transition-colors whitespace-nowrap",
                  active ? "bg-primary-500 text-background-50" : "text-foreground-600",
                )}
              >
                {t.label}
                <span
                  className={cn(
                    "min-w-[18px] h-[18px] px-1 rounded-full text-[10px] font-bold flex items-center justify-center",
                    active ? "bg-background-50/25 text-background-50" : "bg-background-100 text-foreground-700",
                  )}
                >
                  {count(t.id)}
                </span>
              </button>
            );
          })}
        </div>
      </div>

      <div className="px-4 mt-4 space-y-3">
        {list.length === 0 ? (
          <div className="py-16 text-center">
            <div className="w-16 h-16 rounded-full bg-background-200 flex items-center justify-center mx-auto mb-4">
              <i className="ri-shopping-bag-3-line text-[28px] text-foreground-500 leading-none" />
            </div>
            <h3 className="text-[15px] font-semibold text-foreground-950">لا توجد طلبات هنا</h3>
            <p className="text-[12.5px] text-foreground-600 mt-1.5">ستظهر طلباتك في هذا القسم عند توفرها.</p>
          </div>
        ) : (
          list.map((o) => {
            const biz = businesses.find((b) => b.id === o.businessId)!;
            const first = o.items[0];
            return (
              <div
                key={o.id}
                onClick={() => navigate("orderDetails", { orderId: o.number })}
                className="rounded-[20px] bg-background-100 border border-background-200 p-4 cursor-pointer hover:border-background-300 transition-colors"
              >
                <div className="flex items-center justify-between gap-2">
                  <div className="flex items-center gap-2">
                    <span className="text-[12.5px] text-foreground-600">طلب رقم</span>
                    <span className="text-[13.5px] font-bold text-foreground-950" dir="ltr">#{o.number}</span>
                  </div>
                  <Badge tone={o.statusTone}>{o.statusLabel}</Badge>
                </div>
                <div className="flex items-center gap-3 mt-3">
                  <div className="relative shrink-0">
                    <div className="w-14 h-14 rounded-[14px] overflow-hidden bg-background-200">
                      <img src={first.image} alt={first.name} className="w-full h-full object-cover object-top" />
                    </div>
                  </div>
                  <div className="flex-1 min-w-0">
                    <p className="text-[12.5px] font-medium text-foreground-950 truncate">{first.name}</p>
                    {o.itemsCount > 1 && (
                      <p className="text-[11.5px] text-foreground-600 mt-0.5">+{o.itemsCount - 1} منتجات</p>
                    )}
                    <div className="flex items-center gap-1.5 mt-1.5">
                      <div className="w-4 h-4 rounded-full overflow-hidden bg-background-200 shrink-0">
                        <img src={biz.logo} alt={biz.name} className="w-full h-full object-cover object-top" />
                      </div>
                      <span className="text-[11px] text-foreground-600 truncate">{biz.name}</span>
                    </div>
                  </div>
                  <div className="text-left shrink-0">
                    <p className="text-[13.5px] font-bold text-foreground-950">{o.total.toLocaleString("en-US")} ر.س</p>
                  </div>
                </div>
              </div>
            );
          })
        )}
      </div>
    </div>
  );
}