/**
 * Integration Test: Race Stewarding Use Case Orchestration
 * 
 * Tests the orchestration logic of race stewarding page-related Use Cases:
 * - GetLeagueProtestsUseCase: Retrieves comprehensive race stewarding information
 * - ReviewProtestUseCase: Reviews a protest
 * 
 * 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 { InMemoryProtestRepository } from '../../../adapters/racing/persistence/inmemory/InMemoryProtestRepository';
import { InMemoryDriverRepository } from '../../../adapters/racing/persistence/inmemory/InMemoryDriverRepository';
import { InMemoryLeagueRepository } from '../../../adapters/racing/persistence/inmemory/InMemoryLeagueRepository';
import { InMemoryLeagueMembershipRepository } from '../../../adapters/racing/persistence/inmemory/InMemoryLeagueMembershipRepository';
import { GetLeagueProtestsUseCase } from '../../../core/racing/application/use-cases/GetLeagueProtestsUseCase';
import { ReviewProtestUseCase } from '../../../core/racing/application/use-cases/ReviewProtestUseCase';
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 { Protest } from '../../../core/racing/domain/entities/Protest';
import { LeagueMembership } from '../../../core/racing/domain/entities/LeagueMembership';
import { Logger } from '../../../core/shared/domain/Logger';

describe('Race Stewarding Use Case Orchestration', () => {
  let raceRepository: InMemoryRaceRepository;
  let protestRepository: InMemoryProtestRepository;
  let driverRepository: InMemoryDriverRepository;
  let leagueRepository: InMemoryLeagueRepository;
  let leagueMembershipRepository: InMemoryLeagueMembershipRepository;
  let getLeagueProtestsUseCase: GetLeagueProtestsUseCase;
  let reviewProtestUseCase: ReviewProtestUseCase;
  let mockLogger: Logger;

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

    raceRepository = new InMemoryRaceRepository(mockLogger);
    protestRepository = new InMemoryProtestRepository(mockLogger);
    driverRepository = new InMemoryDriverRepository(mockLogger);
    leagueRepository = new InMemoryLeagueRepository(mockLogger);
    leagueMembershipRepository = new InMemoryLeagueMembershipRepository(mockLogger);

    getLeagueProtestsUseCase = new GetLeagueProtestsUseCase(
      raceRepository,
      protestRepository,
      driverRepository,
      leagueRepository
    );

    reviewProtestUseCase = new ReviewProtestUseCase(
      protestRepository,
      raceRepository,
      leagueMembershipRepository
    );
  });

  beforeEach(async () => {
    (raceRepository as any).races.clear();
    (protestRepository as any).protests.clear();
    await driverRepository.clear();
    leagueRepository.clear();
    leagueMembershipRepository.clear();
  });

  describe('GetLeagueProtestsUseCase', () => {
    it('should retrieve league protests with all related entities', async () => {
      // Given: A league, race, drivers and a protest 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(),
        track: 'Spa',
        car: 'GT3',
        status: 'completed'
      });
      await raceRepository.create(race);

      const driver1Id = 'd1';
      const driver2Id = 'd2';
      const driver1 = Driver.create({ id: driver1Id, iracingId: '100', name: 'Protester', country: 'US' });
      const driver2 = Driver.create({ id: driver2Id, iracingId: '200', name: 'Accused', country: 'UK' });
      await driverRepository.create(driver1);
      await driverRepository.create(driver2);

      const protest = Protest.create({
        id: 'p1',
        raceId,
        protestingDriverId: driver1Id,
        accusedDriverId: driver2Id,
        reason: 'Unsafe rejoin',
        timestamp: new Date()
      });
      await protestRepository.create(protest);

      // When: GetLeagueProtestsUseCase.execute() is called
      const result = await getLeagueProtestsUseCase.execute({ leagueId });

      // Then: It should return the protest with race and driver info
      expect(result.isOk()).toBe(true);
      const data = result.unwrap();
      expect(data.protests).toHaveLength(1);
      expect(data.protests[0].protest.id).toBe('p1');
      expect(data.protests[0].race?.id).toBe(raceId);
      expect(data.protests[0].protestingDriver?.id).toBe(driver1Id);
      expect(data.protests[0].accusedDriver?.id).toBe(driver2Id);
    });
  });

  describe('ReviewProtestUseCase', () => {
    it('should allow a steward to review a protest', async () => {
      // Given: A protest and a steward membership
      const leagueId = 'l1';
      const raceId = 'r1';
      const stewardId = 's1';
      
      const race = Race.create({
        id: raceId,
        leagueId,
        scheduledAt: new Date(),
        track: 'Spa',
        car: 'GT3',
        status: 'completed'
      });
      await raceRepository.create(race);

      const protest = Protest.create({
        id: 'p1',
        raceId,
        protestingDriverId: 'd1',
        accusedDriverId: 'd2',
        reason: 'Unsafe rejoin',
        timestamp: new Date()
      });
      await protestRepository.create(protest);

      const membership = LeagueMembership.create({
        id: 'm1',
        leagueId,
        driverId: stewardId,
        role: 'steward',
        status: 'active'
      });
      await leagueMembershipRepository.saveMembership(membership);

      // When: ReviewProtestUseCase.execute() is called
      const result = await reviewProtestUseCase.execute({
        protestId: 'p1',
        stewardId,
        decision: 'accepted',
        comment: 'Clear violation'
      });

      // Then: The protest should be updated
      expect(result.isOk()).toBe(true);
      const updatedProtest = await protestRepository.findById('p1');
      expect(updatedProtest?.status.toString()).toBe('accepted');
      expect(updatedProtest?.reviewedBy).toBe(stewardId);
    });
  });
});
