"use client"

import { create } from "zustand"
import { persist } from "zustand/middleware"
import { debouncedPersistJSONStorage } from "@/lib/stores/debounced-json-storage"

type MailStoreState = {
  seenEmailIds: string[]
  recentMoveTargets: string[]
  recentFolderVisits: string[]
}

type MailStoreActions = {
  markSeen: (id: string) => void
  pushRecentMoveTarget: (targetId: string) => void
  pushRecentFolderVisit: (visitKey: string) => void
}

export const useMailStore = create<MailStoreState & MailStoreActions>()(
  persist(
    (set) => ({
      seenEmailIds: [],
      recentMoveTargets: [],
      recentFolderVisits: [],

      markSeen: (id) =>
        set((s) => ({
          seenEmailIds: s.seenEmailIds.includes(id)
            ? s.seenEmailIds
            : [...s.seenEmailIds, id],
        })),

      pushRecentMoveTarget: (targetId) =>
        set((s) => {
          const MAX = 5
          const filtered = s.recentMoveTargets.filter((t) => t !== targetId)
          return { recentMoveTargets: [targetId, ...filtered].slice(0, MAX) }
        }),

      pushRecentFolderVisit: (visitKey) =>
        set((s) => {
          const MAX = 4
          const filtered = s.recentFolderVisits.filter((k) => k !== visitKey)
          return { recentFolderVisits: [visitKey, ...filtered].slice(0, MAX) }
        }),
    }),
    {
      name: "ultimail-mail-state",
      storage: debouncedPersistJSONStorage,
      version: 4,
      migrate: (persisted) => {
        const state = persisted as Record<string, unknown>
        return {
          seenEmailIds: (state.seenEmailIds as string[]) ?? [],
          recentMoveTargets: (state.recentMoveTargets as string[]) ?? [],
          recentFolderVisits: (state.recentFolderVisits as string[]) ?? [],
        }
      },
    }
  )
)
