import { create } from 'zustand'
import { persist } from 'zustand/middleware'

export type PracticeMode = 'standard' | 'proficient'

interface AppState {
  // UI状态
  sidebarOpen: boolean
  darkMode: boolean
  searchQuery: string
  matrixFullScreen: boolean
  practiceMode: PracticeMode

  // 操作
  toggleSidebar: () => void
  setSidebarOpen: (open: boolean) => void
  toggleDarkMode: () => void
  setDarkMode: (dark: boolean) => void
  setSearchQuery: (query: string) => void
  setMatrixFullScreen: (fullScreen: boolean) => void
  setPracticeMode: (mode: PracticeMode) => void
}

export const useAppStore = create<AppState>()(
  persist(
    (set) => ({
      // 初始状态
      sidebarOpen: true,
      darkMode: false,
      searchQuery: '',
      matrixFullScreen: false,
      practiceMode: 'standard',

      // 操作方法
      toggleSidebar: () => set((state) => ({ sidebarOpen: !state.sidebarOpen })),
      setSidebarOpen: (open) => set({ sidebarOpen: open }),
      toggleDarkMode: () => set((state) => ({ darkMode: !state.darkMode })),
      setDarkMode: (dark) => set({ darkMode: dark }),
      setSearchQuery: (query) => set({ searchQuery: query }),
      setMatrixFullScreen: (fullScreen) => set({ matrixFullScreen: fullScreen }),
      setPracticeMode: (mode) => set({ practiceMode: mode }),
    }),
    {
      name: 'ittoview-app-storage',
      partialize: (state) => ({
        sidebarOpen: state.sidebarOpen,
        darkMode: state.darkMode,
        matrixFullScreen: state.matrixFullScreen,
        practiceMode: state.practiceMode,
        // searchQuery 不持久化到 localStorage，刷新后重置
      }),
    }
  )
)
