import { useApp } from "@/pages/app/AppContext";
import type { ScreenId } from "@/pages/app/types";
import { LogoMark, cn } from "@/pages/app/components/ui";
import { merchantProfile, myBusiness } from "@/mocks/dshnBusiness";

export interface BoMenuItem {
  id: ScreenId;
  label: string;
  icon: string;
}

export const BO_MENU: { title: string; items: BoMenuItem[] }[] = [
  {
    title: "الرئيسية",
    items: [{ id: "boDashboard", label: "لوحة التحكم", icon: "ri-dashboard-3-line" }],
  },
  {
    title: "إدارة",
    items: [
      { id: "boBusiness", label: "إدارة المنشأة", icon: "ri-store-3-line" },
      { id: "boProducts", label: "إدارة المنتجات", icon: "ri-shopping-bag-3-line" },
      { id: "boVerification", label: "التوثيق التجاري", icon: "ri-verified-badge-line" },
    ],
  },
  {
    title: "الاستثمار",
    items: [
      { id: "boOpportunity", label: "فرص الاستثمار", icon: "ri-line-chart-line" },
      { id: "boInvestRequests", label: "طلبات الاستثمار", icon: "ri-user-received-line" },
      { id: "boCoinvest", label: "الاستثمار التشاركي", icon: "ri-group-line" },
      { id: "boCommission", label: "تسوية عمولات الاستثمار", icon: "ri-receipt-line" },
    ],
  },
  {
    title: "المبيعات",
    items: [
      { id: "boOrders", label: "الطلبات المدفوعة", icon: "ri-file-list-3-line" },
      { id: "boNegotiations", label: "طلبات التفاوض", icon: "ri-chat-check-line" },
      { id: "boChats", label: "المحادثات", icon: "ri-chat-3-line" },
    ],
  },
  {
    title: "الشراكات",
    items: [
      { id: "boPartners", label: "استكشاف المنشآت", icon: "ri-search-eye-line" },
      { id: "boMyPartnerships", label: "شراكاتي", icon: "ri-team-line" },
    ],
  },
  {
    title: "الحساب",
    items: [
      { id: "boEarnings", label: "الأرباح والسحب", icon: "ri-wallet-3-line" },
      { id: "boNotifications", label: "الإشعارات", icon: "ri-notification-3-line" },
      { id: "boProfile", label: "الحساب الشخصي", icon: "ri-user-line" },
    ],
  },
];

export default function BoDrawer({
  open,
  onClose,
}: {
  open: boolean;
  onClose: () => void;
}) {
  const { screen, jump } = useApp();

  return (
    <>
      <div
        onClick={onClose}
        className={cn(
          "absolute inset-0 z-40 bg-foreground-950/40 transition-opacity duration-300",
          open ? "opacity-100" : "opacity-0 pointer-events-none",
        )}
      />
      <aside
        className={cn(
          "absolute top-0 right-0 z-50 h-full w-[78%] max-w-[300px] bg-background-50 transition-transform duration-300 flex flex-col",
          open ? "translate-x-0" : "translate-x-full",
        )}
      >
        <div className="p-4 border-b border-background-200 flex items-center justify-between">
          <div className="flex items-center gap-2.5 min-w-0">
            <div className="w-9 h-9 rounded-[10px] bg-primary-500 flex items-center justify-center shrink-0">
              <LogoMark className="text-[14px] text-background-50" />
            </div>
            <div className="min-w-0">
              <p className="text-[13.5px] font-bold text-foreground-950 truncate">{myBusiness.name}</p>
              <p className="text-[11px] text-foreground-600">صاحب بيزنس</p>
            </div>
          </div>
          <button
            type="button"
            onClick={onClose}
            className="w-9 h-9 rounded-full flex items-center justify-center text-foreground-700 hover:bg-background-200 cursor-pointer shrink-0"
          >
            <i className="ri-close-line text-[20px] leading-none" />
          </button>
        </div>

        <div className="flex-1 overflow-y-auto no-scrollbar p-3 space-y-4">
          {BO_MENU.map((g) => (
            <div key={g.title}>
              <p className="px-2 text-[10.5px] font-semibold text-foreground-500 mb-1.5">{g.title}</p>
              <div className="space-y-0.5">
                {g.items.map((it) => {
                  const active = screen === it.id;
                  return (
                    <button
                      key={it.id}
                      type="button"
                      onClick={() => {
                        jump(it.id);
                        onClose();
                      }}
                      className={cn(
                        "w-full flex items-center gap-3 px-3 py-2.5 rounded-[12px] text-[13px] transition-colors cursor-pointer text-right",
                        active
                          ? "bg-primary-500 text-background-50 font-semibold"
                          : "text-foreground-800 hover:bg-background-200",
                      )}
                    >
                      <i className={cn(it.icon, "text-[18px] leading-none shrink-0")} />
                      {it.label}
                    </button>
                  );
                })}
              </div>
            </div>
          ))}
        </div>

        <div className="p-3 border-t border-background-200">
          <div className="flex items-center gap-3 px-2 py-1.5">
            <div className="w-10 h-10 rounded-full overflow-hidden bg-background-200 shrink-0">
              <img
                src={merchantProfile.avatar}
                alt={merchantProfile.name}
                className="w-full h-full object-cover object-top"
              />
            </div>
            <div className="min-w-0">
              <p className="text-[12.5px] font-semibold text-foreground-950 truncate">{merchantProfile.name}</p>
              <p className="text-[11px] text-foreground-600" dir="ltr">{merchantProfile.phone}</p>
            </div>
          </div>
        </div>
      </aside>
    </>
  );
}