import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import { beforeEach, describe, expect, it, Mock, vi } from 'vitest';
import { Protest } from '../../domain/entities/Protest';
import type { LeagueMembershipRepository } from '../../domain/repositories/LeagueMembershipRepository';
import type { ProtestRepository } from '../../domain/repositories/ProtestRepository';
import type { RaceRepository } from '../../domain/repositories/RaceRepository';
import { FileProtestUseCase, type FileProtestErrorCode, type FileProtestInput } from './FileProtestUseCase';

describe('FileProtestUseCase', () => {
  let mockProtestRepo: {
    create: Mock;
  };
  let mockRaceRepo: {
    findById: Mock;
  };
  let mockLeagueMembershipRepo: {
    getLeagueMembers: Mock;
  };

  beforeEach(() => {
    mockProtestRepo = {
      create: vi.fn(),
    };
    mockRaceRepo = {
      findById: vi.fn(),
    };
    mockLeagueMembershipRepo = {
      getLeagueMembers: vi.fn(),
    };
  });

  it('should return error when race does not exist', async () => {
    const useCase = new FileProtestUseCase(
      mockProtestRepo as unknown as ProtestRepository,
      mockRaceRepo as unknown as RaceRepository,
      mockLeagueMembershipRepo as unknown as LeagueMembershipRepository
    );

    mockRaceRepo.findById.mockResolvedValue(null);

    const result = await useCase.execute({
      raceId: 'nonexistent',
      protestingDriverId: 'driver1',
      accusedDriverId: 'driver2',
      incident: { lap: 5, description: 'Collision' },
    } as FileProtestInput);

    expect(result.isErr()).toBe(true);
    const err = result.unwrapErr() as ApplicationErrorCode<FileProtestErrorCode, { message: string }>;
    expect(err.code).toBe('RACE_NOT_FOUND');
    expect(err.details?.message).toBe('Race not found');
  });

  it('should return error when protesting against self', async () => {
    const useCase = new FileProtestUseCase(
      mockProtestRepo as unknown as ProtestRepository,
      mockRaceRepo as unknown as RaceRepository,
      mockLeagueMembershipRepo as unknown as LeagueMembershipRepository
    );

    mockRaceRepo.findById.mockResolvedValue({ id: 'race1', leagueId: 'league1' });

    const result = await useCase.execute({
      raceId: 'race1',
      protestingDriverId: 'driver1',
      accusedDriverId: 'driver1',
      incident: { lap: 5, description: 'Collision' },
    } as FileProtestInput);

    expect(result.isErr()).toBe(true);
    const err = result.unwrapErr() as ApplicationErrorCode<FileProtestErrorCode, { message: string }>;
    expect(err.code).toBe('SELF_PROTEST');
    expect(err.details?.message).toBe('Cannot file a protest against yourself');
  });

  it('should return error when protesting driver is not an active member', async () => {
    const useCase = new FileProtestUseCase(
      mockProtestRepo as unknown as ProtestRepository,
      mockRaceRepo as unknown as RaceRepository,
      mockLeagueMembershipRepo as unknown as LeagueMembershipRepository
    );

    mockRaceRepo.findById.mockResolvedValue({ id: 'race1', leagueId: 'league1' });
    mockLeagueMembershipRepo.getLeagueMembers.mockResolvedValue([
      { driverId: 'driver2', status: 'active' },
    ]);

    const result = await useCase.execute({
      raceId: 'race1',
      protestingDriverId: 'driver1',
      accusedDriverId: 'driver2',
      incident: { lap: 5, description: 'Collision' },
    } as FileProtestInput);

    expect(result.isErr()).toBe(true);
    const err = result.unwrapErr() as ApplicationErrorCode<FileProtestErrorCode, { message: string }>;
    expect(err.code).toBe('NOT_MEMBER');
    expect(err.details?.message).toBe('Protesting driver is not an active member of this league');
  });

  it('should create protest and return protestId on success', async () => {
    const useCase = new FileProtestUseCase(
      mockProtestRepo as unknown as ProtestRepository,
      mockRaceRepo as unknown as RaceRepository,
      mockLeagueMembershipRepo as unknown as LeagueMembershipRepository
    );

    mockRaceRepo.findById.mockResolvedValue({ id: 'race1', leagueId: 'league1' });
    mockLeagueMembershipRepo.getLeagueMembers.mockResolvedValue([
      { driverId: 'driver1', status: 'active' },
    ]);
    mockProtestRepo.create.mockResolvedValue(undefined);

    const result = await useCase.execute({
      raceId: 'race1',
      protestingDriverId: 'driver1',
      accusedDriverId: 'driver2',
      incident: { lap: 5, description: 'Collision' },
      comment: 'Test comment',
      proofVideoUrl: 'http://example.com/video',
    } as FileProtestInput);

    expect(result.isOk()).toBe(true);
    const presented = result.unwrap();
    expect(mockProtestRepo.create).toHaveBeenCalledTimes(1);
    const created = (mockProtestRepo.create as unknown as Mock).mock.calls[0]?.[0] as Protest;

    expect(created.raceId.toString()).toBe('race1');
    expect(created.protestingDriverId.toString()).toBe('driver1');
    expect(created.accusedDriverId.toString()).toBe('driver2');
    expect(created.comment).toBe('Test comment');
    expect(created.proofVideoUrl.toString()).toBe('http://example.com/video');
    expect(created.status.toString()).toBe('pending');

    expect(created.incident.lap.toNumber()).toBe(5);
    expect(created.incident.description.toString()).toBe('Collision');

    expect(presented.protest.raceId.toString()).toBe('race1');
    expect(presented.protest.protestingDriverId.toString()).toBe('driver1');
    expect(presented.protest.accusedDriverId.toString()).toBe('driver2');
    expect(presented.protest.incident.lap.toNumber()).toBe(5);
    expect(presented.protest.incident.description.toString()).toBe('Collision');
    expect(presented.protest.comment).toBe('Test comment');
    expect(presented.protest.proofVideoUrl).toBeDefined();
    expect(presented.protest.proofVideoUrl!.toString()).toBe('http://example.com/video');
  });
});
