import type { LucideIcon } from "lucide-react"
import { ULTICAL_APP_NAME } from "@/lib/suite/page-metadata"
import {
  Bell,
  Bot,
  CalendarDays,
  FolderKanban,
  Monitor,
  Users,
} from "lucide-react"

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

export const MAIL_SETTINGS_BASE_PATH = "/settings"

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_BASE_PATH,
    icon: Monitor,
  },
  {
    id: "accounts",
    label: "Comptes mail",
    description: "IMAP, SMTP, identités d'envoi et signatures",
    href: `${MAIL_SETTINGS_BASE_PATH}/accounts`,
    icon: Users,
  },
  {
    id: "labels",
    label: "Libellés et dossiers",
    description: "Organisation unifiée cross-comptes",
    href: `${MAIL_SETTINGS_BASE_PATH}/labels`,
    icon: FolderKanban,
  },
  {
    id: "notifications",
    label: "Notifications",
    description: "Alertes desktop, mobile et e-mail",
    href: `${MAIL_SETTINGS_BASE_PATH}/notifications`,
    icon: Bell,
  },
  {
    id: "automation",
    label: "Automatisations",
    description: "Règles, webhooks, LLM, recherche web, tokens API",
    href: `${MAIL_SETTINGS_BASE_PATH}/automation`,
    icon: Bot,
  },
  {
    id: "agenda",
    label: ULTICAL_APP_NAME,
    description: "Affichage, visio, invitations, agendas et vues",
    href: `${MAIL_SETTINGS_BASE_PATH}/agenda`,
    icon: CalendarDays,
  },
]

export function isMailSettingsPath(pathname: string | null): boolean {
  return (
    pathname === MAIL_SETTINGS_BASE_PATH ||
    pathname?.startsWith(`${MAIL_SETTINGS_BASE_PATH}/`) === true
  )
}

export function isMailSettingsNavActive(
  pathname: string | null,
  item: MailSettingsNavItem
): boolean {
  if (item.href === MAIL_SETTINGS_BASE_PATH) {
    return (
      pathname === MAIL_SETTINGS_BASE_PATH ||
      pathname === `${MAIL_SETTINGS_BASE_PATH}/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 (!isMailSettingsPath(pathname)) 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 (!isMailSettingsPath(pathname)) return false
  return MAIL_SETTINGS_NAV.some(
    (item) =>
      MAIL_SETTINGS_LEFT_ALIGNED_SECTIONS.includes(item.id) &&
      isMailSettingsNavActive(pathname, item)
  )
}
