/**
 * Tests for authStore — Phase 7 Task 7.7
 *
 * Tests: Initial state, setUser, setTenant, setAuthenticated, setLoading,
 * setError, setPermissions, logout
 */
import { describe, it, expect, beforeEach } from 'vitest';
import { useAuthStore, type User, type Tenant } from '../authStore';

const mockTenant: Tenant = { id: 't1', name: 'Tenant A', slug: 'tenant-a' };
const mockTenant2: Tenant = { id: 't2', name: 'Tenant B', slug: 'tenant-b' };

const mockUser: User = {
  id: 'u1',
  email: 'test@test.com',
  first_name: 'Test',
  last_name: 'User',
  role: 'admin',
  avatar_url: null,
  tenants: [mockTenant, mockTenant2],
};

describe('authStore', () => {
  beforeEach(() => {
    useAuthStore.getState().logout();
  });

  describe('initial state', () => {
    it('starts with null user', () => {
      expect(useAuthStore.getState().user).toBeNull();
    });

    it('starts with null currentTenant', () => {
      expect(useAuthStore.getState().currentTenant).toBeNull();
    });

    it('starts unauthenticated', () => {
      expect(useAuthStore.getState().isAuthenticated).toBe(false);
    });

    it('starts not loading', () => {
      expect(useAuthStore.getState().isLoading).toBe(false);
    });

    it('starts with null error', () => {
      expect(useAuthStore.getState().error).toBeNull();
    });
  });

  describe('setUser', () => {
    it('sets user and marks as authenticated', () => {
      useAuthStore.getState().setUser(mockUser);
      expect(useAuthStore.getState().user).toEqual(mockUser);
      expect(useAuthStore.getState().isAuthenticated).toBe(true);
    });

    it('sets currentTenant to first tenant from user', () => {
      useAuthStore.getState().setUser(mockUser);
      expect(useAuthStore.getState().currentTenant).toEqual(mockTenant);
    });

    it('sets currentTenant to null when user has no tenants', () => {
      useAuthStore.getState().setUser({ ...mockUser, tenants: [] });
      expect(useAuthStore.getState().currentTenant).toBeNull();
    });

    it('sets user to null and unauthenticates', () => {
      useAuthStore.getState().setUser(mockUser);
      useAuthStore.getState().setUser(null);
      expect(useAuthStore.getState().user).toBeNull();
      expect(useAuthStore.getState().isAuthenticated).toBe(false);
      expect(useAuthStore.getState().currentTenant).toBeNull();
    });
  });

  describe('setTenant', () => {
    it('sets current tenant', () => {
      useAuthStore.getState().setTenant(mockTenant2);
      expect(useAuthStore.getState().currentTenant).toEqual(mockTenant2);
    });

    it('sets tenant to null', () => {
      useAuthStore.getState().setTenant(mockTenant);
      useAuthStore.getState().setTenant(null);
      expect(useAuthStore.getState().currentTenant).toBeNull();
    });
  });

  describe('setAuthenticated', () => {
    it('sets authenticated to true', () => {
      useAuthStore.getState().setAuthenticated(true);
      expect(useAuthStore.getState().isAuthenticated).toBe(true);
    });

    it('sets authenticated to false', () => {
      useAuthStore.getState().setAuthenticated(true);
      useAuthStore.getState().setAuthenticated(false);
      expect(useAuthStore.getState().isAuthenticated).toBe(false);
    });
  });

  describe('setLoading', () => {
    it('sets loading to true', () => {
      useAuthStore.getState().setLoading(true);
      expect(useAuthStore.getState().isLoading).toBe(true);
    });

    it('sets loading to false', () => {
      useAuthStore.getState().setLoading(true);
      useAuthStore.getState().setLoading(false);
      expect(useAuthStore.getState().isLoading).toBe(false);
    });
  });

  describe('setError', () => {
    it('sets error message', () => {
      useAuthStore.getState().setError('Login failed');
      expect(useAuthStore.getState().error).toBe('Login failed');
    });

    it('clears error with null', () => {
      useAuthStore.getState().setError('Error');
      useAuthStore.getState().setError(null);
      expect(useAuthStore.getState().error).toBeNull();
    });
  });

  describe('setPermissions', () => {
    it('sets permissions on existing user', () => {
      useAuthStore.getState().setUser(mockUser);
      useAuthStore.getState().setPermissions(['read', 'write'], true, { annual_revenue: 'hidden' });
      const user = useAuthStore.getState().user!;
      expect(user.permissions).toEqual(['read', 'write']);
      expect(user.is_system_admin).toBe(true);
      expect(user.field_permissions).toEqual({ annual_revenue: 'hidden' });
    });

    it('does nothing when user is null', () => {
      useAuthStore.getState().setPermissions(['read'], false, {});
      expect(useAuthStore.getState().user).toBeNull();
    });
  });

  describe('logout', () => {
    it('clears all auth state', () => {
      useAuthStore.getState().setUser(mockUser);
      useAuthStore.getState().setError('Some error');
      useAuthStore.getState().logout();
      expect(useAuthStore.getState().user).toBeNull();
      expect(useAuthStore.getState().currentTenant).toBeNull();
      expect(useAuthStore.getState().isAuthenticated).toBe(false);
      expect(useAuthStore.getState().error).toBeNull();
    });
  });
});
