import { describe, it, expect, vi, Mocked } from 'vitest';
import { AuthService } from './AuthService';
import { AuthApiClient } from '@/lib/api/auth/AuthApiClient';
import { SessionViewModel } from '@/lib/view-models/SessionViewModel';

describe('AuthService', () => {
  let mockApiClient: Mocked<AuthApiClient>;
  let service: AuthService;

  beforeEach(() => {
    mockApiClient = {
      signup: vi.fn(),
      login: vi.fn(),
      logout: vi.fn(),
    } as Mocked<AuthApiClient>;

    service = new AuthService(mockApiClient);
  });

  describe('signup', () => {
    it('should call apiClient.signup and return SessionViewModel', async () => {
      const params = {
        email: 'test@example.com',
        password: 'password123',
        displayName: 'Test User',
      };

      const mockResponse = {
        token: 'jwt-token',
        user: {
          userId: 'user-123',
          email: 'test@example.com',
          displayName: 'Test User',
        },
      };

      mockApiClient.signup.mockResolvedValue(mockResponse);

      const result = await service.signup(params);

      expect(mockApiClient.signup).toHaveBeenCalledWith(params);
      expect(result.isOk()).toBe(true);
      const vm = result.unwrap();
      expect(vm).toBeInstanceOf(SessionViewModel);
      expect(vm.userId).toBe('user-123');
      expect(vm.email).toBe('test@example.com');
      expect(vm.displayName).toBe('Test User');
      expect(vm.isAuthenticated).toBe(true);
    });

    it('should throw error when apiClient.signup fails', async () => {
      const params = {
        email: 'test@example.com',
        password: 'password123',
        displayName: 'Test User',
      };

      const error = new Error('Signup failed');
      mockApiClient.signup.mockRejectedValue(error);

      const result = await service.signup(params);
      expect(result.isErr()).toBe(true);
      expect(result.getError().message).toBe('Signup failed');
    });
  });

  describe('login', () => {
    it('should call apiClient.login and return SessionViewModel', async () => {
      const params = {
        email: 'test@example.com',
        password: 'password123',
      };

      const mockResponse = {
        token: 'jwt-token',
        user: {
          userId: 'user-123',
          email: 'test@example.com',
          displayName: 'Test User',
        },
      };

      mockApiClient.login.mockResolvedValue(mockResponse);

      const result = await service.login(params);

      expect(mockApiClient.login).toHaveBeenCalledWith(params);
      expect(result.isOk()).toBe(true);
      const vm = result.unwrap();
      expect(vm).toBeInstanceOf(SessionViewModel);
      expect(vm.userId).toBe('user-123');
      expect(vm.email).toBe('test@example.com');
      expect(vm.displayName).toBe('Test User');
      expect(vm.isAuthenticated).toBe(true);
    });

    it('should throw error when apiClient.login fails', async () => {
      const params = {
        email: 'test@example.com',
        password: 'password123',
      };

      const error = new Error('Login failed');
      mockApiClient.login.mockRejectedValue(error);

      const result = await service.login(params);
      expect(result.isErr()).toBe(true);
      expect(result.getError().message).toBe('Login failed');
    });
  });

  describe('logout', () => {
    it('should call apiClient.logout', async () => {
      mockApiClient.logout.mockResolvedValue(undefined);

      const result = await service.logout();

      expect(mockApiClient.logout).toHaveBeenCalled();
      expect(result.isOk()).toBe(true);
    });

    it('should throw error when apiClient.logout fails', async () => {
      const error = new Error('Logout failed');
      mockApiClient.logout.mockRejectedValue(error);

      const result = await service.logout();
      expect(result.isErr()).toBe(true);
      expect(result.getError().message).toBe('Logout failed');
    });
  });
});