import type { PermissionKey } from "@/lib/rbac/catalog";

export type NavItem = {
  href: string;
  label: string;
  icon: string;
  /** Sichtbar, wenn der Nutzer mindestens eine dieser Berechtigungen hat. */
  anyOf: PermissionKey[];
};

// Zentrale Navigationsdefinition. Reihenfolge = Anzeigereihenfolge.
export const NAV_ITEMS: NavItem[] = [
  { href: "/dashboard", label: "Dashboard", icon: "dashboard", anyOf: ["dashboard.view"] },
  { href: "/chat", label: "Chat", icon: "chat", anyOf: ["chat.view"] },
  { href: "/kanban", label: "Kanban", icon: "kanban", anyOf: ["kanban.view"] },
  { href: "/projects", label: "Projekte", icon: "projects", anyOf: ["projects.view"] },
  { href: "/creators", label: "Creator", icon: "creators", anyOf: ["creators.view"] },
  { href: "/positions", label: "Ausschreibungen", icon: "list", anyOf: ["forms.view"] },
  { href: "/applications", label: "Bewerbungen", icon: "mail", anyOf: ["forms.view"] },
  { href: "/contact", label: "Kontaktanfragen", icon: "at", anyOf: ["contact.view"] },
  { href: "/time", label: "Arbeitszeit", icon: "time", anyOf: ["time.view"] },
  { href: "/absences", label: "Abwesenheiten", icon: "absences", anyOf: ["absences.view"] },
  // Kalender & Termine sind zusammengeführt.
  { href: "/calendar", label: "Kalender & Termine", icon: "calendar", anyOf: ["calendar.view"] },
  { href: "/vault", label: "Passwörter", icon: "key", anyOf: ["vault.view"] },
  { href: "/cloud", label: "Cloud-Speicher", icon: "cloud", anyOf: ["cloud.view"] },
  { href: "/changelog", label: "Protokolle", icon: "history", anyOf: ["changelog.view"] },
  // Personalverwaltung: nur für Vorgesetzte (dynamisch freigeschaltet, siehe forced).
  { href: "/personnel", label: "Personalverwaltung", icon: "users", anyOf: [] },
  { href: "/admin", label: "Admin", icon: "admin", anyOf: ["admin.access"] },
];

/** Filtert die Navigation anhand der Berechtigungen (plus dynamisch erzwungener Hrefs). */
export function visibleNavItems(
  permissions: readonly string[] | undefined,
  forced?: ReadonlySet<string>,
): NavItem[] {
  const perms = permissions ?? [];
  return NAV_ITEMS.filter(
    (item) => item.anyOf.some((p) => perms.includes(p)) || forced?.has(item.href),
  );
}

// ── Kategorien für die Header-Navigation ────────────────────────────────────
export type NavGroupItem = NavItem & { isNew: boolean };
export type NavGroup = { label: string; icon: string; items: NavGroupItem[] };

const GROUP_DEFS: { label: string; icon: string; hrefs: string[] }[] = [
  // Dashboard steht als direkter Punkt in der Leiste (Gruppe mit nur einem Eintrag).
  { label: "Start", icon: "dashboard", hrefs: ["/dashboard"] },
  { label: "Kommunikation", icon: "chat", hrefs: ["/chat", "/calendar", "/contact"] },
  { label: "Arbeit", icon: "kanban", hrefs: ["/kanban", "/projects"] },
  {
    label: "Personal",
    icon: "time",
    hrefs: ["/time", "/absences", "/creators", "/positions", "/applications"],
  },
  // Alles, was abgelegt und dokumentiert wird.
  { label: "Daten & Ablage", icon: "vault", hrefs: ["/vault", "/cloud", "/changelog"] },
  // Personalverwaltung (Chefs) & Admin gemeinsam unter „Verwaltung".
  { label: "Verwaltung", icon: "admin", hrefs: ["/personnel", "/admin"] },
];

/**
 * Gruppierte, gefilterte Navigation. Leere Gruppen entfallen komplett.
 * `forced` = zusätzlich freigeschaltete Hrefs (z. B. Personalverwaltung für Chefs).
 * `newHrefs` = Module, die als „neu" hervorgehoben werden.
 */
export function visibleNavGroups(
  permissions: readonly string[] | undefined,
  forced?: ReadonlySet<string>,
  newHrefs?: ReadonlySet<string>,
): NavGroup[] {
  const visible = visibleNavItems(permissions, forced);
  const byHref = new Map(visible.map((i) => [i.href, i]));
  const groups: NavGroup[] = [];
  for (const g of GROUP_DEFS) {
    const items = g.hrefs
      .map((h) => byHref.get(h))
      .filter((i): i is NavItem => Boolean(i))
      .map((i) => ({ ...i, isNew: Boolean(newHrefs?.has(i.href)) }));
    if (items.length > 0) groups.push({ label: g.label, icon: g.icon, items });
  }
  return groups;
}

/** Alle als „neu" markierbaren Module (für den Admin-Schalter). */
export const MARKABLE_MODULES = NAV_ITEMS.map((i) => ({
  href: i.href,
  label: i.label,
  icon: i.icon,
}));
