import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
import { CloseRaceEventStewardingUseCase } from './CloseRaceEventStewardingUseCase';
import type { IRaceEventRepository } from '../../domain/repositories/IRaceEventRepository';
import type { IRaceRegistrationRepository } from '../../domain/repositories/IRaceRegistrationRepository';
import type { IPenaltyRepository } from '../../domain/repositories/IPenaltyRepository';
import type { DomainEventPublisher } from '@/shared/domain/DomainEvent';
import type { Logger } from '@core/shared/application';
import { RaceEvent } from '../../domain/entities/RaceEvent';
import { Session } from '../../domain/entities/Session';
import { SessionType } from '../../domain/value-objects/SessionType';

describe('CloseRaceEventStewardingUseCase', () => {
  let useCase: CloseRaceEventStewardingUseCase;
  let raceEventRepository: {
    findAwaitingStewardingClose: Mock;
    update: Mock;
  };
  let raceRegistrationRepository: {
    getRegisteredDrivers: Mock;
  };
  let penaltyRepository: {
    findByRaceId: Mock;
  };
  let domainEventPublisher: {
    publish: Mock;
  };
  let logger: {
    error: Mock;
  };

  beforeEach(() => {
    raceEventRepository = {
      findAwaitingStewardingClose: vi.fn(),
      update: vi.fn(),
    };
    raceRegistrationRepository = {
      getRegisteredDrivers: vi.fn(),
    };
    penaltyRepository = {
      findByRaceId: vi.fn(),
    };
    domainEventPublisher = {
      publish: vi.fn(),
    };
    logger = {
      error: vi.fn(),
    };
    useCase = new CloseRaceEventStewardingUseCase(
      logger as unknown as Logger,
      raceEventRepository as unknown as IRaceEventRepository,
      raceRegistrationRepository as unknown as IRaceRegistrationRepository,
      penaltyRepository as unknown as IPenaltyRepository,
      domainEventPublisher as unknown as DomainEventPublisher,
    );
  });

  it('should close stewarding for expired events successfully', async () => {
    const raceEvent = RaceEvent.create({
      id: 'event-1',
      seasonId: 'season-1',
      leagueId: 'league-1',
      name: 'Test Event',
      sessions: [
        Session.create({
          id: 'session-1',
          raceEventId: 'event-1',
          sessionType: SessionType.main(),
          scheduledAt: new Date(),
          track: 'Test Track',
          car: 'Test Car',
          status: 'completed',
        }),
      ],
      status: 'awaiting_stewarding',
      stewardingClosesAt: new Date(Date.now() - 1000), // expired
    });

    raceEventRepository.findAwaitingStewardingClose.mockResolvedValue([raceEvent]);
    raceRegistrationRepository.getRegisteredDrivers.mockResolvedValue(['driver-1', 'driver-2']);
    penaltyRepository.findByRaceId.mockResolvedValue([]);
    domainEventPublisher.publish.mockResolvedValue(undefined);

    const result = await useCase.execute({});

    expect(result.isOk()).toBe(true);
    expect(raceEventRepository.findAwaitingStewardingClose).toHaveBeenCalled();
    expect(raceEventRepository.update).toHaveBeenCalledWith(
      expect.objectContaining({ id: 'event-1', status: 'closed' })
    );
    expect(domainEventPublisher.publish).toHaveBeenCalled();
  });

  it('should handle no expired events', async () => {
    raceEventRepository.findAwaitingStewardingClose.mockResolvedValue([]);

    const result = await useCase.execute({});

    expect(result.isOk()).toBe(true);
    expect(raceEventRepository.update).not.toHaveBeenCalled();
    expect(domainEventPublisher.publish).not.toHaveBeenCalled();
  });

  it('should return error when repository throws', async () => {
    raceEventRepository.findAwaitingStewardingClose.mockRejectedValue(new Error('DB error'));

    const result = await useCase.execute({});

    expect(result.isErr()).toBe(true);
    expect(result.unwrapErr().code).toBe('FAILED_TO_CLOSE_STEWARDING');
  });
});