import { useState } from "react";
import { useApp } from "../AppContext";
import { BackHeader, Badge, cn } from "../components/ui";
import { rejectedOrdersList } from "@/mocks/dshnOrderRejected";

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

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

/**
 * "طلباتي" list showing a REJECTED order (red badge) alongside a delivered
 * order for contrast. The rejected tab is the "منتهية" tab.
 */
export default function OrdersRejected() {
  const { goBack, navigate } = useApp();
  const [tab, setTab] = useState<Tab>("done");

  const doneList = rejectedOrdersList;
  const list = tab === "done" ? doneList : [];

  const count = (t: Tab) => (t === "done" ? doneList.length : 0);

  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) => (
            <div
              key={o.id}
              onClick={() =>
                o.rejected ? navigate("orderRejected") : navigate("orderDetails", { orderId: o.number })
              }
              className={cn(
                "rounded-[20px] bg-background-100 border p-4 cursor-pointer transition-colors",
                o.rejected ? "border-danger-200" : "border-background-200 hover:border-background-300",
              )}
            >
              <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.tone}>{o.statusLabel}</Badge>
              </div>

              <div className="flex items-center gap-3 mt-3">
                <div className="w-14 h-14 rounded-[14px] overflow-hidden bg-background-200 shrink-0">
                  <img src={o.firstImage} alt={o.firstItemName} className="w-full h-full object-cover object-top" />
                </div>
                <div className="flex-1 min-w-0">
                  <p className="text-[12.5px] font-medium text-foreground-950 leading-snug">{o.firstItemName}</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={o.logo} alt={o.businessName} className="w-full h-full object-cover object-top" />
                    </div>
                    <span className="text-[11px] text-foreground-600 truncate">{o.businessName}</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>

              {o.rejected && (
                <div className="mt-3 pt-3 border-t border-danger-200 flex items-center gap-2">
                  <i className="ri-refund-2-line text-[15px] text-accent-700 leading-none shrink-0" />
                  <span className="text-[11.5px] text-foreground-600">
                    المبلغ المسترد: {o.total.toLocaleString("en-US")} ر.س
                  </span>
                </div>
              )}
            </div>
          ))
        )}
      </div>
    </div>
  );
}