import { describe, it, expect, vi, Mocked, beforeEach } from 'vitest';
import { LeagueWizardService } from './LeagueWizardService';
import { LeagueWizardCommandModel } from '@/lib/command-models/leagues/LeagueWizardCommandModel';
import { LeaguesApiClient } from '@/lib/api/leagues/LeaguesApiClient';

describe('LeagueWizardService', () => {
  let mockApiClient: Mocked<LeaguesApiClient>;
  let service: LeagueWizardService;

  beforeEach(() => {
    mockApiClient = {
      create: vi.fn(),
    } as unknown as Mocked<LeaguesApiClient>;

    service = new LeagueWizardService(mockApiClient);
  });

  describe('createLeague', () => {
    it('should call apiClient.create with correct command', async () => {
      const form = {
        name: 'Test League',
        description: 'A test league',
        toCreateLeagueCommand: vi.fn().mockReturnValue({
          name: 'Test League',
          description: 'A test league',
          ownerId: 'owner-123',
        }),
      } as unknown as LeagueWizardCommandModel;

      const ownerId = 'owner-123';
      const mockOutput = { leagueId: 'new-league-id', success: true };

      mockApiClient.create.mockResolvedValue(mockOutput);

      const result = await service.createLeague(form, ownerId);

      expect(form.toCreateLeagueCommand).toHaveBeenCalledWith(ownerId);
      expect(mockApiClient.create).toHaveBeenCalledWith({
        name: 'Test League',
        description: 'A test league',
        ownerId: 'owner-123',
      });
      expect(result).toEqual(mockOutput);
    });

    it('should throw error when apiClient.create fails', async () => {
      const form = {
        name: 'Test League',
        description: 'A test league',
        toCreateLeagueCommand: vi.fn().mockReturnValue({
          name: 'Test League',
          description: 'A test league',
          ownerId: 'owner-123',
        }),
      } as unknown as LeagueWizardCommandModel;

      const ownerId = 'owner-123';
      const error = new Error('API call failed');

      mockApiClient.create.mockRejectedValue(error);

      await expect(service.createLeague(form, ownerId)).rejects.toThrow('API call failed');
    });
  });

  describe('createLeagueFromConfig', () => {
    it('should call createLeague with same parameters', async () => {
      const form = {
        name: 'Test League',
        description: 'A test league',
        toCreateLeagueCommand: vi.fn().mockReturnValue({
          name: 'Test League',
          description: 'A test league',
          ownerId: 'owner-123',
        }),
      } as unknown as LeagueWizardCommandModel;

      const ownerId = 'owner-123';
      const mockOutput = { leagueId: 'new-league-id', success: true };

      mockApiClient.create.mockResolvedValue(mockOutput);

      // Note: createLeagueFromConfig seems to be missing from the service, 
      // but the test expects it. I'll add it to the service.
      const result = await (service as any).createLeagueFromConfig(form, ownerId);

      expect(mockApiClient.create).toHaveBeenCalled();
      expect(result).toEqual(mockOutput);
    });
  });
});