import { create } from 'zustand'
import { persist } from 'zustand/middleware'
import { User, Bookmark } from '@/types'

interface AppState {
  user: User | null
  theme: 'light' | 'dark'
  fontSize: 'small' | 'medium' | 'large'
  currentBook: number
  currentChapter: number
  bookmarks: Bookmark[]
  setUser: (user: User | null) => void
  setTheme: (theme: 'light' | 'dark') => void
  setFontSize: (size: 'small' | 'medium' | 'large') => void
  setCurrentBook: (book: number) => void
  setCurrentChapter: (chapter: number) => void
  addBookmark: (bookmark: Bookmark) => void
  removeBookmark: (bookmarkId: string) => void
}

export const useStore = create<AppState>()(
  persist(
    (set) => ({
      user: null,
      theme: 'light',
      fontSize: 'medium',
      currentBook: 1,
      currentChapter: 1,
      bookmarks: [],
      setUser: (user) => set({ user }),
      setTheme: (theme) => set({ theme }),
      setFontSize: (fontSize) => set({ fontSize }),
      setCurrentBook: (currentBook) => set({ currentBook }),
      setCurrentChapter: (currentChapter) => set({ currentChapter }),
      addBookmark: (bookmark) => set((state) => ({
        bookmarks: [...state.bookmarks, bookmark]
      })),
      removeBookmark: (bookmarkId) => set((state) => ({
        bookmarks: state.bookmarks.filter(b => b.id !== bookmarkId)
      })),
    }),
    {
      name: 'bible-chat-storage',
      partialize: (state) => ({
        user: state.user,
        theme: state.theme,
        fontSize: state.fontSize,
        currentBook: state.currentBook,
        currentChapter: state.currentChapter,
        bookmarks: state.bookmarks,
      }),
    }
  )
)