/**
 * Integration Test: Race Detail Use Case Orchestration
 * 
 * Tests the orchestration logic of race detail page-related Use Cases:
 * - GetRaceDetailUseCase: Retrieves comprehensive race details
 * 
 * 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 { InMemoryDriverRepository } from '../../../adapters/racing/persistence/inmemory/InMemoryDriverRepository';
import { InMemoryRaceRegistrationRepository } from '../../../adapters/racing/persistence/inmemory/InMemoryRaceRegistrationRepository';
import { InMemoryResultRepository } from '../../../adapters/racing/persistence/inmemory/InMemoryResultRepository';
import { InMemoryLeagueMembershipRepository } from '../../../adapters/racing/persistence/inmemory/InMemoryLeagueMembershipRepository';
import { GetRaceDetailUseCase } from '../../../core/racing/application/use-cases/GetRaceDetailUseCase';
import { Race } from '../../../core/racing/domain/entities/Race';
import { League } from '../../../core/racing/domain/entities/League';
import { Driver } from '../../../core/racing/domain/entities/Driver';
import { Logger } from '../../../core/shared/domain/Logger';

describe('Race Detail Use Case Orchestration', () => {
  let raceRepository: InMemoryRaceRepository;
  let leagueRepository: InMemoryLeagueRepository;
  let driverRepository: InMemoryDriverRepository;
  let raceRegistrationRepository: InMemoryRaceRegistrationRepository;
  let resultRepository: InMemoryResultRepository;
  let leagueMembershipRepository: InMemoryLeagueMembershipRepository;
  let getRaceDetailUseCase: GetRaceDetailUseCase;
  let mockLogger: Logger;

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

    raceRepository = new InMemoryRaceRepository(mockLogger);
    leagueRepository = new InMemoryLeagueRepository(mockLogger);
    driverRepository = new InMemoryDriverRepository(mockLogger);
    raceRegistrationRepository = new InMemoryRaceRegistrationRepository(mockLogger);
    resultRepository = new InMemoryResultRepository(mockLogger, raceRepository);
    leagueMembershipRepository = new InMemoryLeagueMembershipRepository(mockLogger);

    getRaceDetailUseCase = new GetRaceDetailUseCase(
      raceRepository,
      leagueRepository,
      driverRepository,
      raceRegistrationRepository,
      resultRepository,
      leagueMembershipRepository
    );
  });

  beforeEach(async () => {
    // Clear repositories
    (raceRepository as any).races.clear();
    leagueRepository.clear();
    await driverRepository.clear();
    (raceRegistrationRepository as any).registrations.clear();
    (resultRepository as any).results.clear();
    leagueMembershipRepository.clear();
  });

  describe('GetRaceDetailUseCase', () => {
    it('should retrieve race detail with complete information', async () => {
      // Given: A race and league exist
      const leagueId = 'l1';
      const league = League.create({ id: leagueId, name: 'Pro League', description: 'Desc', ownerId: 'o1' });
      await leagueRepository.create(league);

      const raceId = 'r1';
      const race = Race.create({
        id: raceId,
        leagueId,
        scheduledAt: new Date(Date.now() + 86400000),
        track: 'Spa',
        car: 'GT3',
        status: 'scheduled'
      });
      await raceRepository.create(race);

      // When: GetRaceDetailUseCase.execute() is called
      const result = await getRaceDetailUseCase.execute({ raceId });

      // Then: The result should contain race and league information
      expect(result.isOk()).toBe(true);
      const data = result.unwrap();
      expect(data.race.id).toBe(raceId);
      expect(data.league?.id).toBe(leagueId);
      expect(data.isUserRegistered).toBe(false);
    });

    it('should throw error when race does not exist', async () => {
      // When: GetRaceDetailUseCase.execute() is called with non-existent race ID
      const result = await getRaceDetailUseCase.execute({ raceId: 'non-existent' });

      // Then: Should return RACE_NOT_FOUND error
      expect(result.isErr()).toBe(true);
      expect(result.unwrapErr().code).toBe('RACE_NOT_FOUND');
    });

    it('should identify if a driver is registered', async () => {
      // Given: A race and a registered driver
      const leagueId = 'l1';
      const raceId = 'r1';
      const driverId = 'd1';
      
      const race = Race.create({
        id: raceId,
        leagueId,
        scheduledAt: new Date(Date.now() + 86400000),
        track: 'Spa',
        car: 'GT3',
        status: 'scheduled'
      });
      await raceRepository.create(race);

      const driver = Driver.create({ id: driverId, iracingId: '100', name: 'John Doe', country: 'US' });
      await driverRepository.create(driver);

      // Mock registration (using any to bypass private access if needed, but InMemoryRaceRegistrationRepository has register method)
      await raceRegistrationRepository.register({
        raceId: raceId as any,
        driverId: driverId as any,
        registeredAt: new Date()
      } as any);

      // When: GetRaceDetailUseCase.execute() is called with driverId
      const result = await getRaceDetailUseCase.execute({ raceId, driverId });

      // Then: isUserRegistered should be true
      expect(result.isOk()).toBe(true);
      expect(result.unwrap().isUserRegistered).toBe(true);
    });
  });
});
