import { create } from 'zustand';
import { persist } from 'zustand/middleware';
import { AppState, UserState, SpotifyTrack, MixedPlaylist, MemoryLaneItem } from '../types';

interface StoreActions {
  // Authentication actions
  setCurrentUser: (user: UserState) => void;
  setPartnerUser: (user: UserState) => void;
  logout: () => void;
  
  // Loading and error states
  setLoading: (loading: boolean) => void;
  setError: (error: string | null) => void;
  
  // Music playback
  setCurrentTrack: (track: SpotifyTrack | null) => void;
  setIsPlaying: (playing: boolean) => void;
  
  // Mixed playlists
  addMixedPlaylist: (playlist: MixedPlaylist) => void;
  removeMixedPlaylist: (id: string) => void;
  
  // Memory lane
  addMemoryLaneItem: (item: MemoryLaneItem) => void;
  removeMemoryLaneItem: (id: string) => void;
  
  // Theme settings
  setTheme: (theme: string) => void;
  
  // Clear all data
  clearAllData: () => void;
}

type Store = AppState & StoreActions & {
  mixedPlaylists: MixedPlaylist[];
  memoryLane: MemoryLaneItem[];
  theme: string;
};

const initialState: AppState = {
  currentUser: null,
  partnerUser: null,
  isLoading: false,
  error: null,
  currentTrack: null,
  isPlaying: false,
};

export const useStore = create<Store>()(
  persist(
    (set) => ({
      ...initialState,
      mixedPlaylists: [],
      memoryLane: [],
      theme: 'green', // Default theme
      
      setCurrentUser: (user: UserState) => 
        set({ currentUser: user }),
        
      setPartnerUser: (user: UserState) => 
        set({ partnerUser: user }),
        
      logout: () => 
        set({ 
          currentUser: null, 
          partnerUser: null,
          currentTrack: null,
          isPlaying: false,
          mixedPlaylists: [],
          memoryLane: [],
          error: null 
        }),
        
      setLoading: (loading: boolean) => 
        set({ isLoading: loading }),
        
      setError: (error: string | null) => 
        set({ error }),
        
      setCurrentTrack: (track: SpotifyTrack | null) => 
        set({ currentTrack: track }),
        
      setIsPlaying: (playing: boolean) => 
        set({ isPlaying: playing }),
        
      addMixedPlaylist: (playlist: MixedPlaylist) => 
        set((state) => ({ 
          mixedPlaylists: [...state.mixedPlaylists, playlist] 
        })),
        
      removeMixedPlaylist: (id: string) => 
        set((state) => ({ 
          mixedPlaylists: state.mixedPlaylists.filter(p => p.id !== id) 
        })),
        
      addMemoryLaneItem: (item: MemoryLaneItem) => 
        set((state) => ({ 
          memoryLane: [...state.memoryLane, item].sort((a, b) => 
            new Date(b.date).getTime() - new Date(a.date).getTime()
          )
        })),
        
      removeMemoryLaneItem: (id: string) => 
        set((state) => ({ 
          memoryLane: state.memoryLane.filter(item => item.id !== id) 
        })),
        
      setTheme: (theme: string) => 
        set({ theme }),
        
      clearAllData: () => 
        set({
          ...initialState,
          mixedPlaylists: [],
          memoryLane: [],
        }),
    }),
    {
      name: 'spotify-app-settings',
      partialize: (state) => ({ theme: state.theme }),
    }
  )
);
