import { useState } from "react";
import { useApp } from "@/pages/app/AppContext";
import { Badge, cn } from "@/pages/app/components/ui";
import { BizTopBar } from "@/pages/app/components/biz/BizUI";
import BizNegotiationCard from "@/pages/app/components/biz/BizNegotiationCard";
import { merchantOrders, type MerchantOrder } from "@/mocks/dshnBusiness";
import { bizNegotiations } from "@/mocks/dshnBizDeals";

type Segment = "paid" | "negotiations";
type PaidTab = "new" | "current" | "done";

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

function inTab(o: MerchantOrder, t: PaidTab) {
  if (t === "new") return o.status === "new";
  if (t === "current") return o.status === "current";
  return o.status === "done";
}

function EmptyBlock({ title, desc }: { title: string; desc: string }) {
  return (
    <div className="rounded-[18px] bg-background-100 border border-background-200 py-12 flex flex-col items-center text-center">
      <div className="w-14 h-14 rounded-full bg-background-200 flex items-center justify-center mb-3">
        <i className="ri-inbox-line text-[24px] text-foreground-500 leading-none" />
      </div>
      <p className="text-[13.5px] font-semibold text-foreground-950">{title}</p>
      <p className="text-[11.5px] text-foreground-600 mt-1 px-6">{desc}</p>
    </div>
  );
}

export default function BizOrders({ initialSegment = "paid" }: { initialSegment?: Segment }) {
  const { navigate } = useApp();
  const [segment, setSegment] = useState<Segment>(initialSegment);
  const [tab, setTab] = useState<PaidTab>("new");

  const paidList = merchantOrders.filter((o) => inTab(o, tab));

  return (
    <div className="min-h-full bg-background-50 pb-6">
      <BizTopBar title="الطلبات" />

      <div className="px-4 pt-4">
        <div className="flex gap-1 bg-background-100 border border-background-200 rounded-full p-1">
          <button
            type="button"
            onClick={() => setSegment("paid")}
            className={cn(
              "flex-1 h-9 rounded-full text-[12.5px] font-medium whitespace-nowrap cursor-pointer transition-colors",
              segment === "paid" ? "bg-primary-500 text-background-50" : "text-foreground-700",
            )}
          >
            الطلبات المدفوعة ({merchantOrders.length})
          </button>
          <button
            type="button"
            onClick={() => setSegment("negotiations")}
            className={cn(
              "flex-1 h-9 rounded-full text-[12.5px] font-medium whitespace-nowrap cursor-pointer transition-colors",
              segment === "negotiations" ? "bg-primary-500 text-background-50" : "text-foreground-700",
            )}
          >
            طلبات التفاوض ({bizNegotiations.length})
          </button>
        </div>
      </div>

      {segment === "paid" ? (
        <div className="px-4 py-4 space-y-3">
          <div className="flex gap-1 bg-background-100 border border-background-200 rounded-full p-1">
            {PAID_TABS.map((t) => (
              <button
                key={t.id}
                type="button"
                onClick={() => setTab(t.id)}
                className={cn(
                  "flex-1 h-8 rounded-full text-[12px] font-medium whitespace-nowrap cursor-pointer transition-colors",
                  tab === t.id ? "bg-foreground-950 text-background-50" : "text-foreground-700",
                )}
              >
                {t.label}
              </button>
            ))}
          </div>

          {paidList.length === 0 ? (
            <EmptyBlock title="لا توجد طلبات" desc="ستظهر الطلبات المدفوعة هنا حسب حالتها." />
          ) : (
            <div className="space-y-3">
              {paidList.map((o) => (
                <button
                  key={o.id}
                  type="button"
                  onClick={() => navigate("bizOrder", { orderId: o.number })}
                  className="w-full text-right rounded-[18px] bg-background-100 border border-background-200 p-3.5 cursor-pointer hover:border-background-300 transition-colors"
                >
                  <div className="flex items-center gap-3">
                    <div className="w-11 h-11 rounded-[13px] overflow-hidden bg-background-200 shrink-0">
                      <img
                        src={o.items[0].image}
                        alt={o.items[0].name}
                        className="w-full h-full object-cover object-top"
                      />
                    </div>
                    <div className="flex-1 min-w-0">
                      <div className="flex items-center gap-2">
                        <span className="text-[13.5px] font-bold text-foreground-950" dir="ltr">
                          #{o.number}
                        </span>
                        <span className="text-[12px] text-foreground-700 truncate">{o.customer}</span>
                      </div>
                      <p className="text-[11px] text-foreground-600 mt-1">
                        {o.items.length} {o.items.length === 1 ? "منتج" : "منتجات"}
                      </p>
                    </div>
                    <Badge tone={o.statusTone} className="shrink-0">
                      {o.statusLabel}
                    </Badge>
                  </div>
                </button>
              ))}
            </div>
          )}
        </div>
      ) : (
        <div className="px-4 py-4 space-y-3">
          {bizNegotiations.length === 0 ? (
            <EmptyBlock title="لا توجد طلبات تفاوض" desc="ستظهر طلبات التفاوض الجديدة هنا." />
          ) : (
            <div className="space-y-3">
              {bizNegotiations.map((n) => (
                <BizNegotiationCard key={n.id} item={n} />
              ))}
            </div>
          )}
        </div>
      )}
    </div>
  );
}