/**
 * Tests for uiStore — Phase 7 Task 7.7
 *
 * Tests: Initial state, theme, locale, sidebar toggles, AI sidebar,
 * message sidebar, toasts, notifications
 */
import { describe, it, expect, beforeEach } from 'vitest';
import { useUIStore, type Toast } from '../uiStore';

describe('uiStore', () => {
  beforeEach(() => {
    // Reset to default state
    useUIStore.setState({
      theme: 'light',
      locale: 'de',
      sidebarOpen: true,
      suggestionSidebarOpen: false,
      aiSidebarCollapsed: true,
      aiSidebarTab: 'chat',
      messageSidebarCollapsed: true,
      toasts: [],
      notifications: [],
    });
    localStorage.clear();
  });

  describe('initial state', () => {
    it('starts with light theme (when no localStorage)', () => {
      expect(useUIStore.getState().theme).toBe('light');
    });

    it('starts with de locale (when no localStorage)', () => {
      expect(useUIStore.getState().locale).toBe('de');
    });

    it('starts with sidebar open', () => {
      expect(useUIStore.getState().sidebarOpen).toBe(true);
    });

    it('starts with suggestion sidebar closed', () => {
      expect(useUIStore.getState().suggestionSidebarOpen).toBe(false);
    });

    it('starts with AI sidebar collapsed', () => {
      expect(useUIStore.getState().aiSidebarCollapsed).toBe(true);
    });

    it('starts with chat tab for AI sidebar', () => {
      expect(useUIStore.getState().aiSidebarTab).toBe('chat');
    });

    it('starts with message sidebar collapsed', () => {
      expect(useUIStore.getState().messageSidebarCollapsed).toBe(true);
    });

    it('starts with empty toasts', () => {
      expect(useUIStore.getState().toasts).toEqual([]);
    });

    it('starts with empty notifications', () => {
      expect(useUIStore.getState().notifications).toEqual([]);
    });
  });

  describe('setTheme', () => {
    it('sets theme to dark', () => {
      useUIStore.getState().setTheme('dark');
      expect(useUIStore.getState().theme).toBe('dark');
    });

    it('persists theme to localStorage', () => {
      useUIStore.getState().setTheme('dark');
      expect(localStorage.getItem('leocrm_theme')).toBe('dark');
    });

    it('sets theme to system', () => {
      useUIStore.getState().setTheme('system');
      expect(useUIStore.getState().theme).toBe('system');
    });
  });

  describe('setLocale', () => {
    it('sets locale to en', () => {
      useUIStore.getState().setLocale('en');
      expect(useUIStore.getState().locale).toBe('en');
    });

    it('persists locale to localStorage', () => {
      useUIStore.getState().setLocale('en');
      expect(localStorage.getItem('leocrm_lang')).toBe('en');
    });
  });

  describe('toggleSidebar', () => {
    it('toggles sidebar from open to closed', () => {
      useUIStore.getState().toggleSidebar();
      expect(useUIStore.getState().sidebarOpen).toBe(false);
    });

    it('toggles sidebar from closed to open', () => {
      useUIStore.getState().setSidebarOpen(false);
      useUIStore.getState().toggleSidebar();
      expect(useUIStore.getState().sidebarOpen).toBe(true);
    });
  });

  describe('setSidebarOpen', () => {
    it('sets sidebar open explicitly', () => {
      useUIStore.getState().setSidebarOpen(false);
      useUIStore.getState().setSidebarOpen(true);
      expect(useUIStore.getState().sidebarOpen).toBe(true);
    });
  });

  describe('toggleSuggestionSidebar', () => {
    it('toggles suggestion sidebar', () => {
      expect(useUIStore.getState().suggestionSidebarOpen).toBe(false);
      useUIStore.getState().toggleSuggestionSidebar();
      expect(useUIStore.getState().suggestionSidebarOpen).toBe(true);
      useUIStore.getState().toggleSuggestionSidebar();
      expect(useUIStore.getState().suggestionSidebarOpen).toBe(false);
    });
  });

  describe('toggleAISidebar', () => {
    it('toggles AI sidebar collapsed state', () => {
      expect(useUIStore.getState().aiSidebarCollapsed).toBe(true);
      useUIStore.getState().toggleAISidebar();
      expect(useUIStore.getState().aiSidebarCollapsed).toBe(false);
      useUIStore.getState().toggleAISidebar();
      expect(useUIStore.getState().aiSidebarCollapsed).toBe(true);
    });
  });

  describe('setAISidebarCollapsed', () => {
    it('sets AI sidebar collapsed explicitly', () => {
      useUIStore.getState().setAISidebarCollapsed(false);
      expect(useUIStore.getState().aiSidebarCollapsed).toBe(false);
    });
  });

  describe('setAISidebarTab', () => {
    it('sets AI sidebar tab to proactive', () => {
      useUIStore.getState().setAISidebarTab('proactive');
      expect(useUIStore.getState().aiSidebarTab).toBe('proactive');
    });

    it('sets AI sidebar tab to notifications', () => {
      useUIStore.getState().setAISidebarTab('notifications');
      expect(useUIStore.getState().aiSidebarTab).toBe('notifications');
    });
  });

  describe('openAISidebarProactive', () => {
    it('opens AI sidebar and sets tab to proactive', () => {
      useUIStore.getState().setAISidebarCollapsed(true);
      useUIStore.getState().setAISidebarTab('chat');
      useUIStore.getState().openAISidebarProactive();
      expect(useUIStore.getState().aiSidebarCollapsed).toBe(false);
      expect(useUIStore.getState().aiSidebarTab).toBe('proactive');
    });
  });

  describe('toggleMessageSidebar', () => {
    it('toggles message sidebar collapsed state', () => {
      expect(useUIStore.getState().messageSidebarCollapsed).toBe(true);
      useUIStore.getState().toggleMessageSidebar();
      expect(useUIStore.getState().messageSidebarCollapsed).toBe(false);
    });
  });

  describe('setMessageSidebarCollapsed', () => {
    it('sets message sidebar collapsed explicitly', () => {
      useUIStore.getState().setMessageSidebarCollapsed(false);
      expect(useUIStore.getState().messageSidebarCollapsed).toBe(false);
    });
  });

  describe('addToast', () => {
    it('adds a toast to the list', () => {
      useUIStore.getState().addToast({ type: 'success', message: 'Test toast' });
      expect(useUIStore.getState().toasts).toHaveLength(1);
      expect(useUIStore.getState().toasts[0].message).toBe('Test toast');
      expect(useUIStore.getState().toasts[0].type).toBe('success');
    });

    it('generates unique id for each toast', () => {
      useUIStore.getState().addToast({ type: 'info', message: 'Toast 1' });
      useUIStore.getState().addToast({ type: 'info', message: 'Toast 2' });
      expect(useUIStore.getState().toasts).toHaveLength(2);
      expect(useUIStore.getState().toasts[0].id).not.toBe(useUIStore.getState().toasts[1].id);
    });

    it('preserves toast duration when provided', () => {
      useUIStore.getState().addToast({ type: 'warning', message: 'Timed toast', duration: 5000 });
      expect(useUIStore.getState().toasts[0].duration).toBe(5000);
    });
  });

  describe('removeToast', () => {
    it('removes toast by id', () => {
      useUIStore.getState().addToast({ type: 'success', message: 'Toast 1' });
      useUIStore.getState().addToast({ type: 'error', message: 'Toast 2' });
      const idToRemove = useUIStore.getState().toasts[0].id;
      useUIStore.getState().removeToast(idToRemove);
      expect(useUIStore.getState().toasts).toHaveLength(1);
      expect(useUIStore.getState().toasts[0].message).toBe('Toast 2');
    });

    it('does nothing when id does not match', () => {
      useUIStore.getState().addToast({ type: 'success', message: 'Toast 1' });
      useUIStore.getState().removeToast('nonexistent-id');
      expect(useUIStore.getState().toasts).toHaveLength(1);
    });
  });

  describe('clearToasts', () => {
    it('removes all toasts', () => {
      useUIStore.getState().addToast({ type: 'success', message: 'Toast 1' });
      useUIStore.getState().addToast({ type: 'error', message: 'Toast 2' });
      useUIStore.getState().clearToasts();
      expect(useUIStore.getState().toasts).toEqual([]);
    });
  });

  describe('removeNotification', () => {
    it('removes notification by index', () => {
      useUIStore.setState({ notifications: ['notif-1', 'notif-2', 'notif-3'] });
      useUIStore.getState().removeNotification(1);
      expect(useUIStore.getState().notifications).toEqual(['notif-1', 'notif-3']);
    });

    it('removes first notification when index is 0', () => {
      useUIStore.setState({ notifications: ['notif-1', 'notif-2'] });
      useUIStore.getState().removeNotification(0);
      expect(useUIStore.getState().notifications).toEqual(['notif-2']);
    });
  });

  describe('clearNotifications', () => {
    it('removes all notifications', () => {
      useUIStore.setState({ notifications: ['notif-1', 'notif-2'] });
      useUIStore.getState().clearNotifications();
      expect(useUIStore.getState().notifications).toEqual([]);
    });
  });
});
