import { describe, it, expect, beforeEach, vi } from 'vitest';
import { useAuthStore } from '@/features/auth/store/authStore';
import { useUIStore } from '@/stores/ui';
import { useChatStore } from '@/features/chat/store/chatStore';
import { useLibraryStore } from '@/stores/library';
import { usePlayerStore } from '@/stores/player';

// Mock localStorage
const localStorageMock = {
  getItem: vi.fn(),
  setItem: vi.fn(),
  removeItem: vi.fn(),
  clear: vi.fn(),
};
Object.defineProperty(window, 'localStorage', {
  value: localStorageMock,
});

describe('Zustand Stores', () => {
  beforeEach(() => {
    // Reset stores before each test
    useAuthStore.getState().logout();
    useUIStore.getState().clearNotifications();
    // Action 4.5.1.5: Chat store moved to feature store - no setConnected method
    // Action 4.6.1.12: Library store no longer has clearItems (domain data migrated to React Query)
    usePlayerStore.getState().clearQueue();
  });

  describe('Auth Store', () => {
    it('should have initial state', () => {
      const state = useAuthStore.getState();
      // Action 4.1.1.5: User data migrated to React Query - auth store only tracks isAuthenticated
      expect(state.isAuthenticated).toBe(false);
      // isLoading peut être true si le store est en cours de rehydration
      expect(typeof state.isLoading).toBe('boolean');
      expect(state.error).toBeNull();
    });

    it('should handle login action', () => {
      const { login } = useAuthStore.getState();
      expect(typeof login).toBe('function');
    });

    it('should handle logout action', () => {
      const { logout } = useAuthStore.getState();
      expect(typeof logout).toBe('function');
    });

    it('should handle clearError action', () => {
      const { clearError } = useAuthStore.getState();
      expect(typeof clearError).toBe('function');
    });

    it('should set authentication state on login', () => {
      // Action 4.1.1.5: User data migrated to React Query - auth store only tracks isAuthenticated
      const { isAuthenticated } = useAuthStore.getState();
      expect(typeof isAuthenticated).toBe('boolean');
    });

    it('should clear authentication state on logout', () => {
      const { logout } = useAuthStore.getState();

      // Set initial state
      useAuthStore.setState({
        isAuthenticated: true,
      });

      logout();

      const state = useAuthStore.getState();
      // Action 4.1.1.5: User data migrated to React Query
      expect(state.isAuthenticated).toBe(false);
    });
  });

  describe('UI Store', () => {
    it('should have initial state', () => {
      const state = useUIStore.getState();
      expect(state.theme).toBe('system');
      expect(state.language).toBe('en');
      expect(state.sidebarOpen).toBe(true);
      expect(state.notifications).toEqual([]);
    });

    it('should handle theme changes', () => {
      const { setTheme } = useUIStore.getState();
      setTheme('dark');
      expect(useUIStore.getState().theme).toBe('dark');
    });

    it('should handle language changes', () => {
      const { setLanguage } = useUIStore.getState();
      setLanguage('fr');
      expect(useUIStore.getState().language).toBe('fr');
    });

    it('should handle sidebar toggle', () => {
      const { setSidebarOpen } = useUIStore.getState();
      setSidebarOpen(false);
      expect(useUIStore.getState().sidebarOpen).toBe(false);
    });
  });

  describe('Chat Store', () => {
    it('should have initial state', () => {
      const state = useChatStore.getState();
      // Action 4.5.1.5: Chat store moved to feature store - updated structure
      expect(state.conversations).toEqual([]);
      expect(state.currentConversationId).toBeNull();
      expect(state.messages).toEqual({});
      expect(state.typingUsers).toEqual({});
      expect(state.wsStatus).toBe('disconnected');
      expect(state.userId).toBeNull();
      expect(state.username).toBeNull();
    });

    it('should handle conversation actions', () => {
      const { addConversation, setCurrentConversation } =
        useChatStore.getState();
      expect(typeof addConversation).toBe('function');
      expect(typeof setCurrentConversation).toBe('function');
    });

    it('should handle message actions', () => {
      const { addMessage, loadMessages } = useChatStore.getState();
      expect(typeof addMessage).toBe('function');
      expect(typeof loadMessages).toBe('function');
    });
  });

  describe('Library Store', () => {
    it('should have initial state', () => {
      const state = useLibraryStore.getState();
      // Action 4.1.2.6: Domain data migrated to React Query - only UI state remains
      expect(state.filters).toEqual({});
    });

    it('should handle filter actions', () => {
      const { setFilters } = useLibraryStore.getState();
      expect(typeof setFilters).toBe('function');
      
      setFilters({ type: 'track', search: 'test' });
      const state = useLibraryStore.getState();
      expect(state.filters.type).toBe('track');
      expect(state.filters.search).toBe('test');
    });
  });

  describe('Player Store', () => {
    it('should have initial state', () => {
      const state = usePlayerStore.getState();
      expect(state.currentTrack).toBeNull();
      expect(state.isPlaying).toBe(false);
      expect(state.currentTime).toBe(0);
      expect(state.duration).toBe(0);
      expect(state.volume).toBe(100);
      expect(state.muted).toBe(false);
      expect(state.queue).toEqual([]);
      expect(state.currentIndex).toBe(-1);
      expect(state.repeat).toBe('off');
      expect(state.shuffle).toBe(false);
    });

    it('should handle play action', () => {
      const { play } = usePlayerStore.getState();
      const mockTrack = {
        id: '1',
        title: 'Test Track',
        artist: 'Test Artist',
        duration: 180,
        url: 'https://example.com/track.mp3',
      } as any;

      play(mockTrack);
      const state = usePlayerStore.getState();
      expect(state.currentTrack).toEqual(mockTrack);
      expect(state.isPlaying).toBe(true);
    });

    it('should handle pause action', () => {
      const { pause } = usePlayerStore.getState();
      pause();
      expect(usePlayerStore.getState().isPlaying).toBe(false);
    });

    it('should handle resume action', () => {
      const { resume } = usePlayerStore.getState();
      resume();
      expect(usePlayerStore.getState().isPlaying).toBe(true);
    });

    it('should handle volume changes', () => {
      const { setVolume } = usePlayerStore.getState();
      setVolume(50);
      expect(usePlayerStore.getState().volume).toBe(50);
    });

    it('should handle toggle mute', () => {
      const { toggleMute } = usePlayerStore.getState();
      const initialMuted = usePlayerStore.getState().muted;
      toggleMute();
      expect(usePlayerStore.getState().muted).toBe(!initialMuted);
    });

    it('should handle add to queue', () => {
      const { addToQueue } = usePlayerStore.getState();
      const mockTrack = {
        id: '1',
        title: 'Test Track',
      } as any;

      addToQueue(mockTrack);
      const state = usePlayerStore.getState();
      expect(state.queue).toHaveLength(1);
      expect(state.queue[0]).toEqual(mockTrack);
    });

    it('should handle clear queue', () => {
      const { addToQueue, clearQueue } = usePlayerStore.getState();
      const mockTrack = { id: '1' } as any;

      addToQueue(mockTrack);
      clearQueue();

      const state = usePlayerStore.getState();
      expect(state.queue).toEqual([]);
      expect(state.currentIndex).toBe(-1);
      expect(state.currentTrack).toBeNull();
    });

    it('should handle toggle shuffle', () => {
      const { toggleShuffle } = usePlayerStore.getState();
      const initialShuffle = usePlayerStore.getState().shuffle;
      toggleShuffle();
      expect(usePlayerStore.getState().shuffle).toBe(!initialShuffle);
    });

    it('should handle set repeat', () => {
      const { setRepeat } = usePlayerStore.getState();
      setRepeat('playlist');
      expect(usePlayerStore.getState().repeat).toBe('playlist');
    });
  });
});
