import type { LucideIcon } from "lucide-react"
import {
  Bot,
  Calendar,
  Video,
  FileCog,
  Gauge,
  Link2,
  LayoutDashboard,
  Mail,
  Puzzle,
  ScrollText,
  Search,
  Shield,
  ShieldCheck,
  Users,
} from "lucide-react"

export type AdminSettingsSectionId =
  | "overview"
  | "users"
  | "authentication"
  | "security"
  | "quotas"
  | "file-policies"
  | "public-shares"
  | "llm"
  | "search"
  | "plugins"
  | "mail-domains"
  | "ai-assistant"
  | "agenda"
  | "ultimeet"
  | "audit"

export type AdminSettingsNavItem = {
  id: AdminSettingsSectionId
  label: string
  description: string
  href: string
  icon: LucideIcon
}

export const ADMIN_SETTINGS_NAV: AdminSettingsNavItem[] = [
  {
    id: "overview",
    label: "Vue d'ensemble",
    description: "Statistiques et activité de la plateforme",
    href: "/admin/settings",
    icon: LayoutDashboard,
  },
  {
    id: "users",
    label: "Utilisateurs",
    description: "Comptes, types d'accès, invitations et quotas",
    href: "/admin/settings/users",
    icon: Users,
  },
  {
    id: "authentication",
    label: "Authentification",
    description: "Authentik, SSO et provisionnement",
    href: "/admin/settings/authentication",
    icon: Shield,
  },
  {
    id: "security",
    label: "Sécurité",
    description: "Politiques 2FA et exigences d'accès",
    href: "/admin/settings/security",
    icon: ShieldCheck,
  },
  {
    id: "quotas",
    label: "Quotas",
    description: "Stockage, LLM, recherche web et automatisations",
    href: "/admin/settings/quotas",
    icon: Gauge,
  },
  {
    id: "file-policies",
    label: "Politiques fichiers",
    description: "Upload, partage et rétention",
    href: "/admin/settings/file-policies",
    icon: FileCog,
  },
  {
    id: "public-shares",
    label: "Partages externes",
    description: "Liens publics Drive et audit d'accès",
    href: "/admin/settings/public-shares",
    icon: Link2,
  },
  {
    id: "search",
    label: "Moteur de recherche",
    description: "Index suite et recherche web",
    href: "/admin/settings/search",
    icon: Search,
  },
  {
    id: "plugins",
    label: "Plugins",
    description: "Nextcloud, modules et intégrations activables",
    href: "/admin/settings/plugins",
    icon: Puzzle,
  },
  {
    id: "agenda",
    label: "Agenda",
    description: "Thème et visioconférence par défaut",
    href: "/admin/settings/agenda",
    icon: Calendar,
  },
  {
    id: "ultimeet",
    label: "UltiMeet",
    description: "Transcription, moteurs STT et actions post-réunion",
    href: "/admin/settings/ultimeet",
    icon: Video,
  },
  {
    id: "mail-domains",
    label: "Mail",
    description: "Domaines hébergés, SMTP et migration",
    href: "/admin/settings/mail-domains",
    icon: Mail,
  },
  {
    id: "ai-assistant",
    label: "UltiAI",
    description: "Assistant IA, fournisseurs LLM et tools suite",
    href: "/admin/settings/ai-assistant",
    icon: Bot,
  },
  {
    id: "audit",
    label: "Journal d'audit",
    description: "Actions administratives et export",
    href: "/admin/settings/audit",
    icon: ScrollText,
  },
]

export function isAdminSettingsNavActive(
  pathname: string | null,
  item: AdminSettingsNavItem
): boolean {
  if (item.href === "/admin/settings") {
    return pathname === "/admin/settings" || pathname === "/admin/settings/overview"
  }
  return (
    pathname === item.href || Boolean(pathname?.startsWith(`${item.href}/`))
  )
}

export function resolveAdminSettingsSection(
  segments: string[] | undefined
): AdminSettingsSectionId {
  const slug = segments?.[0]
  if (slug === "llm") return "ai-assistant"
  if (slug === "nextcloud" || slug === "onlyoffice" || slug === "richtext") return "plugins"
  if (slug === "mailing") return "mail-domains"
  if (slug === "storage-quotas" || slug === "usage-quotas") return "quotas"
  const match = ADMIN_SETTINGS_NAV.find((item) => {
    if (item.id === "overview") return !slug || slug === "overview"
    return item.href.endsWith(`/${slug}`)
  })
  return match?.id ?? "overview"
}

const ADMIN_WIDE_SECTIONS: AdminSettingsSectionId[] = [
  "overview",
  "audit",
  "ai-assistant",
  "plugins",
  "quotas",
  "search",
  "mail-domains",
  "authentication",
  "security",
  "file-policies",
]

const ADMIN_FULL_WIDTH_SECTIONS: AdminSettingsSectionId[] = [
  "users",
  "public-shares",
]

export function isAdminSettingsWideLayoutPath(pathname: string | null): boolean {
  if (!pathname?.startsWith("/admin/settings")) return false
  return ADMIN_SETTINGS_NAV.some(
    (item) =>
      ADMIN_WIDE_SECTIONS.includes(item.id) &&
      isAdminSettingsNavActive(pathname, item)
  )
}

export function isAdminSettingsFullWidthLayoutPath(pathname: string | null): boolean {
  if (!pathname?.startsWith("/admin/settings")) return false
  return ADMIN_SETTINGS_NAV.some(
    (item) =>
      ADMIN_FULL_WIDTH_SECTIONS.includes(item.id) &&
      isAdminSettingsNavActive(pathname, item)
  )
}
