import type { LucideIcon } from "lucide-react"
import {
  Bell,
  Bot,
  CalendarDays,
  FolderKanban,
  Monitor,
  Users,
} from "lucide-react"

export type MailSettingsSectionId =
  | "display"
  | "accounts"
  | "labels"
  | "notifications"
  | "automation"
  | "agenda"

export type MailSettingsNavItem = {
  id: MailSettingsSectionId
  label: string
  description: string
  href: string
  icon: LucideIcon
}

export const MAIL_SETTINGS_NAV: MailSettingsNavItem[] = [
  {
    id: "display",
    label: "Affichage",
    description: "Densité, thème, boîte de réception, volet de lecture",
    href: "/mail/settings",
    icon: Monitor,
  },
  {
    id: "accounts",
    label: "Comptes mail",
    description: "IMAP, SMTP, identités d'envoi et signatures",
    href: "/mail/settings/accounts",
    icon: Users,
  },
  {
    id: "labels",
    label: "Libellés et dossiers",
    description: "Organisation unifiée cross-comptes",
    href: "/mail/settings/labels",
    icon: FolderKanban,
  },
  {
    id: "notifications",
    label: "Notifications",
    description: "Alertes desktop, mobile et e-mail",
    href: "/mail/settings/notifications",
    icon: Bell,
  },
  {
    id: "automation",
    label: "Automatisations",
    description: "Règles, webhooks, LLM, recherche web, tokens API",
    href: "/mail/settings/automation",
    icon: Bot,
  },
  {
    id: "agenda",
    label: "Agenda",
    description: "Affichage, visio, invitations, agendas et vues",
    href: "/mail/settings/agenda",
    icon: CalendarDays,
  },
]

export function isMailSettingsNavActive(
  pathname: string | null,
  item: MailSettingsNavItem
): boolean {
  if (item.href === "/mail/settings") {
    return (
      pathname === "/mail/settings" || pathname === "/mail/settings/display"
    )
  }
  return (
    pathname === item.href || Boolean(pathname?.startsWith(`${item.href}/`))
  )
}

export function resolveMailSettingsSection(
  segments: string[] | undefined
): MailSettingsSectionId {
  const slug = segments?.[0]
  const match = MAIL_SETTINGS_NAV.find((item) => {
    if (item.id === "display") return !slug || slug === "display"
    return item.href.endsWith(`/${slug}`)
  })
  return match?.id ?? "display"
}

const MAIL_SETTINGS_WIDE_LAYOUT_SECTIONS: MailSettingsSectionId[] = [
  "display",
  "automation",
  "agenda",
]

const MAIL_SETTINGS_LEFT_ALIGNED_SECTIONS: MailSettingsSectionId[] = ["accounts"]

export function isMailSettingsWideLayoutPath(pathname: string | null): boolean {
  if (!pathname?.startsWith("/mail/settings")) return false
  return MAIL_SETTINGS_NAV.some(
    (item) =>
      MAIL_SETTINGS_WIDE_LAYOUT_SECTIONS.includes(item.id) &&
      isMailSettingsNavActive(pathname, item)
  )
}

export function isMailSettingsLeftAlignedPath(pathname: string | null): boolean {
  if (!pathname?.startsWith("/mail/settings")) return false
  return MAIL_SETTINGS_NAV.some(
    (item) =>
      MAIL_SETTINGS_LEFT_ALIGNED_SECTIONS.includes(item.id) &&
      isMailSettingsNavActive(pathname, item)
  )
}
