import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import { beforeEach, describe, expect, it, Mock, vi } from 'vitest';
import type { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';
import type { IProtestRepository } from '../../domain/repositories/IProtestRepository';
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
import { FileProtestUseCase, type FileProtestErrorCode, type FileProtestInput, type FileProtestResult } from './FileProtestUseCase';

describe('FileProtestUseCase', () => {
  let mockProtestRepo: {
    create: Mock;
  };
  let mockRaceRepo: {
    findById: Mock;
  };
  let mockLeagueMembershipRepo: {
    getLeagueMembers: Mock;
  };
  let output: UseCaseOutputPort<FileProtestResult> & { present: Mock };

  beforeEach(() => {
    mockProtestRepo = {
      create: vi.fn(),
    };
    mockRaceRepo = {
      findById: vi.fn(),
    };
    mockLeagueMembershipRepo = {
      getLeagueMembers: vi.fn(),
    };
    output = {
      present: vi.fn(),
    } as unknown as UseCaseOutputPort<FileProtestResult> & { present: Mock };
  });

  it('should return error when race does not exist', async () => {
    const useCase = new FileProtestUseCase(
      mockProtestRepo as unknown as IProtestRepository,
      mockRaceRepo as unknown as IRaceRepository,
      mockLeagueMembershipRepo as unknown as ILeagueMembershipRepository,
      output,
    );

    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');
    expect(output.present).not.toHaveBeenCalled();
  });

  it('should return error when protesting against self', async () => {
    const useCase = new FileProtestUseCase(
      mockProtestRepo as unknown as IProtestRepository,
      mockRaceRepo as unknown as IRaceRepository,
      mockLeagueMembershipRepo as unknown as ILeagueMembershipRepository,
      output,
    );

    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');
    expect(output.present).not.toHaveBeenCalled();
  });

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

    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');
    expect(output.present).not.toHaveBeenCalled();
  });

  it('should create protest and return protestId on success', async () => {
    const useCase = new FileProtestUseCase(
      mockProtestRepo as unknown as IProtestRepository,
      mockRaceRepo as unknown as IRaceRepository,
      mockLeagueMembershipRepo as unknown as ILeagueMembershipRepository,
      output,
    );

    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);
    expect(result.unwrap()).toBeUndefined();
    expect(mockProtestRepo.create).toHaveBeenCalledTimes(1);
    const created = (mockProtestRepo.create as unknown as Mock).mock.calls[0]?.[0] as unknown as {
      raceId: { toString(): string };
      protestingDriverId: { toString(): string };
      accusedDriverId: { toString(): string };
      comment?: string;
      proofVideoUrl: { toString(): string };
      status: { toString(): string };
      incident: {
        lap: { toNumber(): number };
        description: { toString(): string };
        timeInRace?: unknown;
      };
    };

    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(created.incident.timeInRace).toBeUndefined();

    expect(output.present).toHaveBeenCalledTimes(1);
    const presented = (output.present as unknown as Mock).mock.calls[0]?.[0] as FileProtestResult;
    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.incident.timeInRace).toBeUndefined();
    expect(presented.protest.comment).toBe('Test comment');
    expect(presented.protest.proofVideoUrl).toBeDefined();
    expect(presented.protest.proofVideoUrl!.toString()).toBe('http://example.com/video');
  });
});