/**
 * Integration Test: Races Main Use Case Orchestration
 * 
 * Tests the orchestration logic of races main page-related Use Cases:
 * - GetAllRacesUseCase: Used to retrieve upcoming and recent 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('Races Main 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('Races Main Page Data', () => {
    it('should retrieve upcoming and recent races', async () => {
      // Given: Upcoming and completed races exist
      const leagueId = 'l1';
      const league = League.create({ id: leagueId, name: 'Pro League', description: 'Desc', ownerId: 'o1' });
      await leagueRepository.create(league);

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

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

      // Then: The result should contain both races
      expect(result.isOk()).toBe(true);
      const data = result.unwrap();
      expect(data.races).toHaveLength(2);
      expect(data.races.some(r => r.status.isScheduled())).toBe(true);
      expect(data.races.some(r => r.status.isCompleted())).toBe(true);
    });
  });
});
