import type { MailThemeMode } from "@/lib/mail-settings/types"
import { destinationEmailFromStoredKey } from "@/lib/agenda/agenda-destination-identities"

export type AgendaTimeFormat = "24h" | "12h"

export type AgendaDurationStep = 5 | 10 | 15 | 20 | 30 | 60

export type AgendaVideoProvider =
  | "ultimeet"
  | "google_meet"
  | "zoom"
  | "teams"
  | "jitsi"
  | "none"

/** 0 = dimanche, 1 = lundi, 6 = samedi ; auto = locale navigateur. */
export type AgendaWeekStart = "auto" | 0 | 1 | 6

export type AgendaInvitationExclusion =
  | { type: "email"; value: string; label: string }
  | { type: "contact"; value: string; label: string }
  | { type: "identity"; value: string; label: string }
  | { type: "folder"; value: string; label: string }
  | { type: "label"; value: string; label: string }
  | { type: "mailbox"; value: string; label: string }
  | { type: "not_in_contacts"; value: string; label: string }

export type AgendaAutoImportSource =
  | Extract<AgendaInvitationExclusion, { type: "identity" }>
  | Extract<AgendaInvitationExclusion, { type: "contact" }>

/** Agenda abonné via URL iCal (stocké côté client). */
export type AgendaExternalCalendar = {
  id: string
  display_name: string
  url: string
  color: string
  /** Compte mail rattaché (null = tous les comptes). */
  account_id: string | null
}

/** Vue nommée : sous-ensemble d'agendas (et libellés mail associés). */
export type AgendaCalendarView = {
  id: string
  name: string
  calendar_ids: string[]
  label_ids: string[]
}

export type AgendaUserSettings = {
  defaultVideoProvider: AgendaVideoProvider
  /** Clés API utilisateur (si non imposées par l'organisation). */
  videoProviderApiKeys: Partial<Record<AgendaVideoProvider, string>>
  defaultInvitationIdentityKey: string | null
  /** Mails de destination ou contacts expéditeurs déclenchant l'import automatique. */
  autoImportInvitationSources: AgendaAutoImportSource[]
  /** Règles d'exclusion de l'import automatique des invitations. */
  invitationImportExclusions: AgendaInvitationExclusion[]
  weekStart: AgendaWeekStart
  defaultQuickDurationMinutes: number
  /** Minutes depuis minuit (0 = 00:00). */
  visibleHoursStart: number
  /** Minutes depuis minuit (1439 = 23:59). */
  visibleHoursEnd: number
  timeFormat: AgendaTimeFormat
  dragSnapMinutes: AgendaDurationStep
  buttonSnapMinutes: AgendaDurationStep
  /** Agendas iCal externes. */
  externalCalendars: AgendaExternalCalendar[]
  /** Vues d'agendas enregistrées. */
  calendarViews: AgendaCalendarView[]
  /** Vue appliquée par défaut au démarrage (`null` = aucune vue). */
  defaultCalendarViewId: string | null
  /**
   * Agendas activés par compte mail (`accountId → calendarIds`).
   * Absent = tous les agendas activés pour ce compte.
   */
  accountEnabledCalendarIds: Record<string, string[]>
}

export type AgendaOrgSettings = {
  default_theme_mode: MailThemeMode
  enforce_org_theme: boolean
  default_video_provider: AgendaVideoProvider
  enforce_org_video_provider: boolean
  video_provider_api_keys: Partial<Record<AgendaVideoProvider, string>>
}

export type AgendaOrgAgendaPublic = {
  default_theme_mode: MailThemeMode
  enforce_org_theme: boolean
  default_video_provider: AgendaVideoProvider
  enforce_org_video_provider: boolean
  configured_video_providers: AgendaVideoProvider[]
}

export const AGENDA_DURATION_STEPS: AgendaDurationStep[] = [5, 10, 15, 20, 30, 60]

export const AGENDA_VIDEO_PROVIDERS: AgendaVideoProvider[] = [
  "ultimeet",
  "google_meet",
  "zoom",
  "teams",
  "jitsi",
  "none",
]

export const AGENDA_VIDEO_PROVIDER_LABELS: Record<AgendaVideoProvider, string> = {
  ultimeet: "UltiMeet",
  google_meet: "Google Meet",
  zoom: "Zoom",
  teams: "Microsoft Teams",
  jitsi: "Jitsi",
  none: "Aucun",
}

export const AGENDA_QUICK_DURATION_OPTIONS = [
  { minutes: 15, label: "15 min" },
  { minutes: 30, label: "30 min" },
  { minutes: 45, label: "45 min" },
  { minutes: 60, label: "1 h" },
  { minutes: 90, label: "1 h 30" },
  { minutes: 120, label: "2 h" },
] as const

export const AGENDA_WEEK_START_OPTIONS: { value: AgendaWeekStart; label: string }[] = [
  { value: "auto", label: "Automatique (locale)" },
  { value: 1, label: "Lundi" },
  { value: 0, label: "Dimanche" },
  { value: 6, label: "Samedi" },
]

export function exclusionKey(item: AgendaInvitationExclusion): string {
  if (item.type === "identity") {
    return `identity:${destinationEmailFromStoredKey(item.value)}`
  }
  return `${item.type}:${item.value}`
}
