import type { ComponentType } from "react";
import {
  Award,
  BarChart3,
  BellRing,
  BookOpen,
  Brain,
  Calendar,
  CheckSquare,
  Clock,
  Crown,
  FileText,
  FolderKanban,
  Inbox,
  LayoutDashboard,
  LayoutGrid,
  MessageSquareText,
  Network,
  PiggyBank,
  UserRoundSearch,
  Receipt,
  Sparkles,
  Target,
  Timer,
  TrendingUp,
  Tv2,
  Users,
  Users2,
  Wallet,
  Zap,
} from "lucide-react";

export type SidebarNavItem = {
  id: string;
  name: string;
  href: string;
  icon: ComponentType<{ className?: string }>;
};

export type SidebarNavGroup = {
  id: string;
  label: string;
  color: string;
  items: SidebarNavItem[];
};

export type NavigationPresetId = "balanced" | "productivity" | "growth" | "business" | "finance";

export type NavigationPreset = {
  id: NavigationPresetId;
  name: string;
  description: string;
  favoriteIds: string[];
  mobileDockIds: string[];
  hiddenIds: string[];
};

export const sidebarTopNav: SidebarNavItem[] = [
  { id: "ai", name: "AI", href: "/ai", icon: Sparkles },
  { id: "dashboard", name: "Dashboard", href: "/", icon: LayoutDashboard },
  { id: "command-center", name: "Command Center", href: "/command-center", icon: Zap },
  { id: "life-map", name: "Life Map", href: "/life-map", icon: Network },
  { id: "inbox", name: "Inbox", href: "/inbox", icon: Inbox },
  { id: "chat", name: "Chat", href: "/chat", icon: MessageSquareText },
  { id: "focus", name: "Focus", href: "/focus", icon: Timer },
];

export const sidebarNavGroups: SidebarNavGroup[] = [
  {
    id: "productivity",
    label: "Produktivität",
    color: "text-sky-400/60",
    items: [
      { id: "focus-rooms", name: "Focus Rooms", href: "/focus/rooms", icon: Users },
      { id: "notes", name: "Notes", href: "/notes", icon: FileText },
      { id: "tasks", name: "Tasks", href: "/tasks", icon: CheckSquare },
      { id: "projects", name: "Projects", href: "/projects", icon: FolderKanban },
      { id: "calendar", name: "Calendar", href: "/calendar", icon: Calendar },
      { id: "reminders", name: "Reminders", href: "/reminders", icon: BellRing },
      { id: "leads", name: "Leads", href: "/leads", icon: UserRoundSearch },
      { id: "vault", name: "Vault", href: "/vault", icon: FileText },
    ],
  },
  {
    id: "growth",
    label: "Wachstum",
    color: "text-emerald-400/60",
    items: [
      { id: "mind-map", name: "Mind Map", href: "/mind-map", icon: Brain },
      { id: "journal", name: "Journal", href: "/journal", icon: BookOpen },
      { id: "habits", name: "Habits", href: "/habits", icon: Target },
      { id: "achievements", name: "Achievements", href: "/achievements", icon: Award },
      { id: "mindset", name: "Mindset", href: "/mindset", icon: TrendingUp },
    ],
  },
  {
    id: "finances",
    label: "Finanzen",
    color: "text-amber-400/60",
    items: [
      { id: "finances", name: "Finances", href: "/finances", icon: Wallet },
      { id: "net-worth", name: "Net Worth", href: "/net-worth", icon: PiggyBank },
      { id: "invoicing", name: "Invoicing", href: "/invoicing", icon: Receipt },
      { id: "insights", name: "Insights", href: "/insights", icon: BarChart3 },
      { id: "teams", name: "Teams", href: "/teams", icon: Users2 },
    ],
  },
  {
    id: "extras",
    label: "Extras",
    color: "text-white/20",
    items: [
      { id: "wrapped", name: "Wrapped", href: "/wrapped", icon: Tv2 },
      { id: "time-capsule", name: "Time Capsule", href: "/time-capsule", icon: Clock },
      { id: "vision-board", name: "Vision Board", href: "/vision-board", icon: LayoutGrid },
    ],
  },
  {
    id: "system",
    label: "System",
    color: "text-violet-400/60",
    items: [
      { id: "automations", name: "Automations", href: "/automations", icon: Zap },
      { id: "admin", name: "Admin", href: "/admin", icon: Crown },
    ],
  },
];

export const sidebarNavItems = [
  ...sidebarTopNav,
  ...sidebarNavGroups.flatMap((group) => group.items),
];

export const sidebarNavItemIds = sidebarNavItems.map((item) => item.id);

export const sidebarNavItemById = new Map(sidebarNavItems.map((item) => [item.id, item]));

export const defaultFavoriteSidebarItemIds = [
  "dashboard",
  "life-map",
  "inbox",
  "tasks",
  "journal",
  "focus",
  "finances",
];

export const defaultMobileDockItemIds = [
  "dashboard",
  "inbox",
  "tasks",
  "journal",
  "focus",
];

export const navigationPresets: NavigationPreset[] = [
  {
    id: "balanced",
    name: "Balanced",
    description: "Daily work, reflection, money, and capture.",
    favoriteIds: defaultFavoriteSidebarItemIds,
    mobileDockIds: defaultMobileDockItemIds,
    hiddenIds: ["admin", "developers"],
  },
  {
    id: "productivity",
    name: "Productivity",
    description: "Tasks, projects, calendar, focus, and capture first.",
    favoriteIds: ["dashboard", "command-center", "inbox", "tasks", "projects", "calendar", "focus"],
    mobileDockIds: ["dashboard", "inbox", "tasks", "calendar", "focus"],
    hiddenIds: ["wrapped", "time-capsule", "vision-board", "admin", "developers"],
  },
  {
    id: "growth",
    name: "Self Growth",
    description: "Journal, habits, mindset, goals, and achievements.",
    favoriteIds: ["dashboard", "life-map", "journal", "habits", "mindset", "achievements", "mind-map"],
    mobileDockIds: ["dashboard", "life-map", "journal", "habits", "mindset"],
    hiddenIds: ["leads", "invoicing", "developers", "admin"],
  },
  {
    id: "business",
    name: "Business",
    description: "Operations, teams, clients, invoices, and automation.",
    favoriteIds: ["dashboard", "command-center", "inbox", "projects", "teams", "leads", "invoicing"],
    mobileDockIds: ["dashboard", "inbox", "projects", "teams", "invoicing"],
    hiddenIds: ["wrapped", "time-capsule", "vision-board", "admin"],
  },
  {
    id: "finance",
    name: "Finance",
    description: "Cashflow, net worth, invoices, and analytics.",
    favoriteIds: ["dashboard", "life-map", "finances", "net-worth", "invoicing", "insights", "analytics"],
    mobileDockIds: ["dashboard", "life-map", "finances", "net-worth", "invoicing"],
    hiddenIds: ["chat", "focus-rooms", "wrapped", "time-capsule", "vision-board", "admin", "developers"],
  },
];

export function getValidSidebarItemIds(itemIds: string[]) {
  return itemIds.filter((id) => sidebarNavItemIds.includes(id));
}

export function orderSidebarItems<T extends SidebarNavItem>(items: T[], orderedIds: string[]) {
  const order = new Map(orderedIds.map((id, index) => [id, index]));
  return [...items].sort((a, b) => {
    const aOrder = order.get(a.id) ?? Number.MAX_SAFE_INTEGER;
    const bOrder = order.get(b.id) ?? Number.MAX_SAFE_INTEGER;
    if (aOrder !== bOrder) return aOrder - bOrder;
    return sidebarNavItemIds.indexOf(a.id) - sidebarNavItemIds.indexOf(b.id);
  });
}
