import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
import { RequestProtestDefenseUseCase } from './RequestProtestDefenseUseCase';
import type { IProtestRepository } from '../../domain/repositories/IProtestRepository';
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
import type { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';

describe('RequestProtestDefenseUseCase', () => {
  let useCase: RequestProtestDefenseUseCase;
  let protestRepository: { findById: Mock; update: Mock };
  let raceRepository: { findById: Mock };
  let membershipRepository: { getMembership: Mock };

  beforeEach(() => {
    protestRepository = { findById: vi.fn(), update: vi.fn() };
    raceRepository = { findById: vi.fn() };
    membershipRepository = { getMembership: vi.fn() };
    useCase = new RequestProtestDefenseUseCase(
      protestRepository as unknown as IProtestRepository,
      raceRepository as unknown as IRaceRepository,
      membershipRepository as unknown as ILeagueMembershipRepository,
    );
  });

  it('should return protest not found error', async () => {
    protestRepository.findById.mockResolvedValue(null);

    const result = await useCase.execute({
      protestId: 'protest-1',
      stewardId: 'steward-1',
    });

    expect(result.isErr()).toBe(true);
    expect(result.unwrapErr()).toEqual({ code: 'PROTEST_NOT_FOUND' });
  });

  it('should return race not found error', async () => {
    const mockProtest = { raceId: 'race-1', accusedDriverId: 'driver-1', id: 'protest-1', canRequestDefense: vi.fn().mockReturnValue(true) };
    protestRepository.findById.mockResolvedValue(mockProtest);
    raceRepository.findById.mockResolvedValue(null);

    const result = await useCase.execute({
      protestId: 'protest-1',
      stewardId: 'steward-1',
    });

    expect(result.isErr()).toBe(true);
    expect(result.unwrapErr()).toEqual({ code: 'RACE_NOT_FOUND' });
  });

  it('should return insufficient permissions error', async () => {
    const mockProtest = { raceId: 'race-1', accusedDriverId: 'driver-1', id: 'protest-1', canRequestDefense: vi.fn().mockReturnValue(true) };
    const mockRace = { leagueId: 'league-1' };
    protestRepository.findById.mockResolvedValue(mockProtest);
    raceRepository.findById.mockResolvedValue(mockRace);
    membershipRepository.getMembership.mockResolvedValue(null);

    const result = await useCase.execute({
      protestId: 'protest-1',
      stewardId: 'steward-1',
    });

    expect(result.isErr()).toBe(true);
    expect(result.unwrapErr()).toEqual({ code: 'INSUFFICIENT_PERMISSIONS' });
  });

  it('should return defense cannot be requested error', async () => {
    const mockProtest = { raceId: 'race-1', accusedDriverId: 'driver-1', id: 'protest-1', canRequestDefense: vi.fn().mockReturnValue(false) };
    const mockRace = { leagueId: 'league-1' };
    const mockMembership = { role: 'steward' };
    protestRepository.findById.mockResolvedValue(mockProtest);
    raceRepository.findById.mockResolvedValue(mockRace);
    membershipRepository.getMembership.mockResolvedValue(mockMembership);

    const result = await useCase.execute({
      protestId: 'protest-1',
      stewardId: 'steward-1',
    });

    expect(result.isErr()).toBe(true);
    expect(result.unwrapErr()).toEqual({ code: 'DEFENSE_CANNOT_BE_REQUESTED' });
  });

  it('should request defense successfully', async () => {
    const mockProtest = {
      raceId: 'race-1',
      accusedDriverId: 'driver-1',
      id: 'protest-1',
      canRequestDefense: vi.fn().mockReturnValue(true),
      requestDefense: vi.fn().mockReturnValue({}),
    };
    const mockRace = { leagueId: 'league-1' };
    const mockMembership = { role: 'steward' };
    protestRepository.findById.mockResolvedValue(mockProtest);
    raceRepository.findById.mockResolvedValue(mockRace);
    membershipRepository.getMembership.mockResolvedValue(mockMembership);
    protestRepository.update.mockResolvedValue(undefined);

    const result = await useCase.execute({
      protestId: 'protest-1',
      stewardId: 'steward-1',
    });

    expect(result.isOk()).toBe(true);
    expect(result.unwrap()).toEqual({
      success: true,
      accusedDriverId: 'driver-1',
      protestId: 'protest-1',
    });
    expect(protestRepository.update).toHaveBeenCalledWith(mockProtest.requestDefense());
  });
});