"use client"

import { create } from "zustand"
import { persist } from "zustand/middleware"
import { debouncedPersistJSONStorage } from "@/lib/stores/debounced-json-storage"
import { normalizeMailBackgroundId } from "@/lib/mail-settings/constants"
import { defaultNotificationPrefs } from "@/lib/mail-settings/map-api-settings"
import type {
  InboxSortMode,
  MailBackgroundId,
  MailDensity,
  MailThemeMode,
  ReadingPaneMode,
} from "@/lib/mail-settings/types"
import {
  LIST_PAGE_SIZE,
  isListPageSize,
  normalizeListPageSize,
  type ListPageSize,
} from "@/lib/mail-list-page-size"

type MailSettingsState = {
  quickSettingsOpen: boolean
  themeDialogOpen: boolean
  density: MailDensity
  themeMode: MailThemeMode
  backgroundId: MailBackgroundId
  inboxSort: InboxSortMode
  readingPane: ReadingPaneMode
  conversationMode: boolean
  infiniteScroll: boolean
  listPageSize: ListPageSize
  desktopNewMail: boolean
  desktopMentions: boolean
  emailDigest: boolean
  soundEnabled: boolean
}

type MailSettingsActions = {
  setQuickSettingsOpen: (open: boolean) => void
  setThemeDialogOpen: (open: boolean) => void
  setDensity: (density: MailDensity) => void
  setThemeMode: (mode: MailThemeMode) => void
  setBackgroundId: (id: MailBackgroundId) => void
  setInboxSort: (sort: InboxSortMode) => void
  setReadingPane: (mode: ReadingPaneMode) => void
  setConversationMode: (enabled: boolean) => void
  setInfiniteScroll: (enabled: boolean) => void
  setListPageSize: (size: ListPageSize) => void
  setDesktopNewMail: (enabled: boolean) => void
  setDesktopMentions: (enabled: boolean) => void
  setEmailDigest: (enabled: boolean) => void
  setSoundEnabled: (enabled: boolean) => void
  hydrateFromApi: (settings: Partial<
    Pick<
      MailSettingsState,
      | "density"
      | "themeMode"
      | "backgroundId"
      | "inboxSort"
      | "readingPane"
      | "conversationMode"
      | "desktopNewMail"
      | "desktopMentions"
      | "emailDigest"
      | "soundEnabled"
    >
  >) => void
}

const defaults: MailSettingsState = {
  quickSettingsOpen: false,
  themeDialogOpen: false,
  density: "default",
  themeMode: "system",
  backgroundId: "none",
  inboxSort: "default",
  readingPane: "none",
  conversationMode: true,
  infiniteScroll: false,
  listPageSize: LIST_PAGE_SIZE,
  ...defaultNotificationPrefs,
}

export const useMailSettingsStore = create<
  MailSettingsState & MailSettingsActions
>()(
  persist(
    (set) => ({
      ...defaults,

      setQuickSettingsOpen: (open) => set({ quickSettingsOpen: open }),
      setThemeDialogOpen: (open) => set({ themeDialogOpen: open }),
      setDensity: (density) => set({ density }),
      setThemeMode: (themeMode) => set({ themeMode }),
      setBackgroundId: (backgroundId) => set({ backgroundId }),
      setInboxSort: (inboxSort) => set({ inboxSort }),
      setReadingPane: (readingPane) => set({ readingPane }),
      setConversationMode: (conversationMode) => set({ conversationMode }),
      setInfiniteScroll: (infiniteScroll) => set({ infiniteScroll }),
      setListPageSize: (listPageSize) => set({ listPageSize }),
      setDesktopNewMail: (desktopNewMail) => set({ desktopNewMail }),
      setDesktopMentions: (desktopMentions) => set({ desktopMentions }),
      setEmailDigest: (emailDigest) => set({ emailDigest }),
      setSoundEnabled: (soundEnabled) => set({ soundEnabled }),
      hydrateFromApi: (settings) =>
        set((current) => ({
          ...current,
          ...settings,
          ...(settings.backgroundId !== undefined
            ? {
                backgroundId: normalizeMailBackgroundId(
                  settings.backgroundId as string
                ),
              }
            : {}),
        })),
    }),
    {
      name: "ultimail-mail-settings",
      storage: debouncedPersistJSONStorage,
      partialize: (s) => ({
        density: s.density,
        themeMode: s.themeMode,
        backgroundId: s.backgroundId,
        inboxSort: s.inboxSort,
        readingPane: s.readingPane,
        conversationMode: s.conversationMode,
        infiniteScroll: s.infiniteScroll,
        listPageSize: s.listPageSize,
        desktopNewMail: s.desktopNewMail,
        desktopMentions: s.desktopMentions,
        emailDigest: s.emailDigest,
        soundEnabled: s.soundEnabled,
      }),
      merge: (persisted, current) => {
        const p = persisted as Partial<MailSettingsState> | undefined
        if (!p) return current
        return {
          ...current,
          ...p,
          backgroundId: normalizeMailBackgroundId(
            (p.backgroundId as string) ?? "none"
          ),
          listPageSize: isListPageSize(Number(p.listPageSize))
            ? normalizeListPageSize(Number(p.listPageSize))
            : current.listPageSize,
        }
      },
    }
  )
)
