/**
 * Integration Test: All Races Use Case Orchestration
 * 
 * Tests the orchestration logic of all races page-related Use Cases:
 * - GetAllRacesUseCase: Retrieves comprehensive list of all races
 * 
 * Adheres to Clean Architecture:
 * - Tests Core Use Cases directly
 * - Uses In-Memory adapters for repositories
 * - Follows Given/When/Then pattern
 * 
 * Focus: Business logic orchestration, NOT UI rendering
 */

import { describe, it, expect, beforeAll, beforeEach } from 'vitest';
import { InMemoryRaceRepository } from '../../../adapters/racing/persistence/inmemory/InMemoryRaceRepository';
import { InMemoryLeagueRepository } from '../../../adapters/racing/persistence/inmemory/InMemoryLeagueRepository';
import { GetAllRacesUseCase } from '../../../core/racing/application/use-cases/GetAllRacesUseCase';
import { Race } from '../../../core/racing/domain/entities/Race';
import { League } from '../../../core/racing/domain/entities/League';
import { Logger } from '../../../core/shared/domain/Logger';

describe('All Races Use Case Orchestration', () => {
  let raceRepository: InMemoryRaceRepository;
  let leagueRepository: InMemoryLeagueRepository;
  let getAllRacesUseCase: GetAllRacesUseCase;
  let mockLogger: Logger;

  beforeAll(() => {
    mockLogger = {
      info: () => {},
      debug: () => {},
      warn: () => {},
      error: () => {},
    } as unknown as Logger;

    raceRepository = new InMemoryRaceRepository(mockLogger);
    leagueRepository = new InMemoryLeagueRepository(mockLogger);

    getAllRacesUseCase = new GetAllRacesUseCase(
      raceRepository,
      leagueRepository,
      mockLogger
    );
  });

  beforeEach(async () => {
    (raceRepository as any).races.clear();
    leagueRepository.clear();
  });

  describe('GetAllRacesUseCase', () => {
    it('should retrieve comprehensive list of all races', async () => {
      // Given: Multiple races exist
      const leagueId = 'l1';
      const league = League.create({ id: leagueId, name: 'Pro League', description: 'Desc', ownerId: 'o1' });
      await leagueRepository.create(league);

      const race1 = Race.create({
        id: 'r1',
        leagueId,
        scheduledAt: new Date(Date.now() + 86400000),
        track: 'Spa',
        car: 'GT3',
        status: 'scheduled'
      });
      const race2 = Race.create({
        id: 'r2',
        leagueId,
        scheduledAt: new Date(Date.now() - 86400000),
        track: 'Monza',
        car: 'GT3',
        status: 'completed'
      });
      await raceRepository.create(race1);
      await raceRepository.create(race2);

      // When: GetAllRacesUseCase.execute() is called
      const result = await getAllRacesUseCase.execute({});

      // Then: The result should contain all races and leagues
      expect(result.isOk()).toBe(true);
      const data = result.unwrap();
      expect(data.races).toHaveLength(2);
      expect(data.leagues).toHaveLength(1);
      expect(data.totalCount).toBe(2);
    });

    it('should return empty list when no races exist', async () => {
      // When: GetAllRacesUseCase.execute() is called
      const result = await getAllRacesUseCase.execute({});

      // Then: The result should be empty
      expect(result.isOk()).toBe(true);
      expect(result.unwrap().races).toHaveLength(0);
      expect(result.unwrap().totalCount).toBe(0);
    });
  });
});
