import { useApp } from "../AppContext";
import type { ScreenId } from "../types";
import { cn } from "./ui";

interface TabDef {
  id: ScreenId;
  label: string;
  icon: string;
  activeIcon: string;
}

const TABS: TabDef[] = [
  { id: "home", label: "الرئيسية", icon: "ri-home-5-line", activeIcon: "ri-home-5-fill" },
  { id: "businesses", label: "المنشآت", icon: "ri-store-2-line", activeIcon: "ri-store-2-fill" },
  { id: "products", label: "المنتجات", icon: "ri-shopping-bag-3-line", activeIcon: "ri-shopping-bag-3-fill" },
  { id: "chats", label: "المحادثات", icon: "ri-chat-3-line", activeIcon: "ri-chat-3-fill" },
  { id: "profile", label: "حسابي", icon: "ri-user-3-line", activeIcon: "ri-user-3-fill" },
];

export default function TabBar() {
  const { activeTab, jump, isGuest } = useApp();

  const items = isGuest ? TABS.filter((t) => t.id === "home" || t.id === "businesses" || t.id === "products") : TABS;

  return (
    <div className="shrink-0 bg-background-100 border-t border-background-200">
      <div className="h-[72px] flex items-stretch">
        {items.map((tab) => {
          const active = activeTab === tab.id;
          return (
            <button
              key={tab.id}
              type="button"
              onClick={() => jump(tab.id)}
              className="flex-1 flex flex-col items-center justify-center gap-1 cursor-pointer"
            >
              <i
                className={cn(
                  active ? tab.activeIcon : tab.icon,
                  "text-[24px] leading-none transition-colors",
                  active ? "text-foreground-950" : "text-foreground-500",
                )}
              />
              <span
                className={cn(
                  "text-[12px] leading-none whitespace-nowrap",
                  active ? "text-foreground-950 font-semibold" : "text-foreground-500 font-medium",
                )}
              >
                {tab.label}
              </span>
            </button>
          );
        })}

        {isGuest && (
          <button
            type="button"
            onClick={() => jump("welcome")}
            className="flex-1 flex flex-col items-center justify-center gap-1 cursor-pointer"
          >
            <i className="ri-user-3-line text-[24px] leading-none text-foreground-500" />
            <span className="text-[12px] leading-none whitespace-nowrap text-foreground-500 font-medium">
              تسجيل / دخول
            </span>
          </button>
        )}
      </div>
    </div>
  );
}