import type { AgendaCalendarView, AgendaExternalCalendar } from "@/lib/agenda/agenda-settings-types"
import type { AgendaCalendar } from "@/lib/agenda/agenda-types"

export const EXTERNAL_CALENDAR_ID_PREFIX = "ext:"

export function isExternalCalendarId(id: string): boolean {
  return id.startsWith(EXTERNAL_CALENDAR_ID_PREFIX)
}

export function externalCalendarToAgendaCalendar(
  external: AgendaExternalCalendar,
): AgendaCalendar {
  return {
    id: external.id,
    display_name: external.display_name,
    color: external.color,
    path: external.url,
  }
}

export function mergeAgendaCalendars(
  apiCalendars: AgendaCalendar[],
  externalCalendars: AgendaExternalCalendar[],
): AgendaCalendar[] {
  return [
    ...apiCalendars,
    ...externalCalendars.map(externalCalendarToAgendaCalendar),
  ]
}

export function isCalendarEnabledForAccount(
  calendarId: string,
  accountId: string,
  accountEnabledCalendarIds: Record<string, string[]>,
): boolean {
  const enabled = accountEnabledCalendarIds[accountId]
  if (!enabled) return true
  return enabled.includes(calendarId)
}

export function isCalendarEnabledForAnyAccount(
  calendarId: string,
  accountIds: string[],
  accountEnabledCalendarIds: Record<string, string[]>,
  scopedAccountId: string | null = null,
): boolean {
  if (accountIds.length === 0) return true
  const relevantAccountIds = scopedAccountId
    ? accountIds.filter((accountId) => accountId === scopedAccountId)
    : accountIds
  if (scopedAccountId && relevantAccountIds.length === 0) return false
  return relevantAccountIds.some((accountId) =>
    isCalendarEnabledForAccount(calendarId, accountId, accountEnabledCalendarIds),
  )
}

export function resolveActiveCalendarView(
  calendarViews: AgendaCalendarView[],
  activeCalendarViewId: string | null,
): AgendaCalendarView | null {
  if (!activeCalendarViewId) return null
  return calendarViews.find((view) => view.id === activeCalendarViewId) ?? null
}

export function resolveVisibleCalendarIds({
  allCalendars,
  hiddenCalendarIds,
  accountIds,
  accountEnabledCalendarIds,
  activeView,
}: {
  allCalendars: Array<{ id: string; account_id?: string | null }>
  hiddenCalendarIds: string[]
  accountIds: string[]
  accountEnabledCalendarIds: Record<string, string[]>
  activeView: AgendaCalendarView | null
}): string[] {
  const hidden = new Set(hiddenCalendarIds)

  return allCalendars
    .map((calendar) => calendar.id)
    .filter((calendarId) => {
      if (hidden.has(calendarId)) return false
      const scopedAccountId =
        allCalendars.find((calendar) => calendar.id === calendarId)?.account_id ?? null
      if (
        !isCalendarEnabledForAnyAccount(
          calendarId,
          accountIds,
          accountEnabledCalendarIds,
          scopedAccountId,
        )
      ) {
        return false
      }
      if (activeView && !activeView.calendar_ids.includes(calendarId)) {
        return false
      }
      return true
    })
}

export function filterAgendaCalendars(
  calendars: AgendaCalendar[],
  visibleIds: string[],
): AgendaCalendar[] {
  const visible = new Set(visibleIds)
  return calendars.filter((calendar) => visible.has(calendar.id))
}

/** Libellé sidebar pour désactiver une vue — réservé, pas de vue enregistrée. */
export const ALL_AGENDAS_VIEW_LABEL = "Tous les agendas"

export function isReservedAgendaViewName(name: string): boolean {
  return (
    name.trim().toLocaleLowerCase("fr") ===
    ALL_AGENDAS_VIEW_LABEL.toLocaleLowerCase("fr")
  )
}
