import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
import { ReviewProtestUseCase, type ReviewProtestInput, type ReviewProtestResult, type ReviewProtestErrorCode } from './ReviewProtestUseCase';
import type { IProtestRepository } from '../../domain/repositories/IProtestRepository';
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
import type { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type { Logger, UseCaseOutputPort } from '@core/shared/application';

describe('ReviewProtestUseCase', () => {
  let useCase: ReviewProtestUseCase;
  let protestRepository: { findById: Mock; update: Mock };
  let raceRepository: { findById: Mock };
  let leagueMembershipRepository: { getLeagueMembers: Mock };
  let logger: { debug: Mock; info: Mock; warn: Mock; error: Mock };
  let output: UseCaseOutputPort<ReviewProtestResult> & { present: Mock };

  beforeEach(() => {
    protestRepository = { findById: vi.fn(), update: vi.fn() };
    raceRepository = { findById: vi.fn() };
    leagueMembershipRepository = { getLeagueMembers: vi.fn() };
    logger = {
      debug: vi.fn(),
      info: vi.fn(),
      warn: vi.fn(),
      error: vi.fn(),
    };
    output = { present: vi.fn() } as unknown as UseCaseOutputPort<ReviewProtestResult> & { present: Mock };
    useCase = new ReviewProtestUseCase(
      protestRepository as unknown as IProtestRepository,
      raceRepository as unknown as IRaceRepository,
      leagueMembershipRepository as unknown as ILeagueMembershipRepository,
      logger as unknown as Logger,
      output,
    );
  });

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

    const input: ReviewProtestInput = {
      protestId: 'protest-1',
      stewardId: 'steward-1',
      decision: 'uphold',
      decisionNotes: 'Notes',
    };

    const result = await useCase.execute(input);

    expect(result.isErr()).toBe(true);
    const error = result.unwrapErr() as ApplicationErrorCode<ReviewProtestErrorCode, { message: string }>;
    expect(error).toEqual({
      code: 'PROTEST_NOT_FOUND',
      details: { message: 'Protest not found' },
    });
    expect(output.present).not.toHaveBeenCalled();
  });

  it('should return race not found error', async () => {
    const mockProtest = { raceId: 'race-1' };
    protestRepository.findById.mockResolvedValue(mockProtest);
    raceRepository.findById.mockResolvedValue(null);

    const input: ReviewProtestInput = {
      protestId: 'protest-1',
      stewardId: 'steward-1',
      decision: 'uphold',
      decisionNotes: 'Notes',
    };

    const result = await useCase.execute(input);

    expect(result.isErr()).toBe(true);
    const error = result.unwrapErr() as ApplicationErrorCode<ReviewProtestErrorCode, { message: string }>;
    expect(error).toEqual({
      code: 'RACE_NOT_FOUND',
      details: { message: 'Race not found' },
    });
    expect(output.present).not.toHaveBeenCalled();
  });

  it('should return not league admin error', async () => {
    const mockProtest = { raceId: 'race-1', uphold: vi.fn(), dismiss: vi.fn() };
    const mockRace = { leagueId: 'league-1' };
    protestRepository.findById.mockResolvedValue(mockProtest);
    raceRepository.findById.mockResolvedValue(mockRace);
    leagueMembershipRepository.getLeagueMembers.mockResolvedValue([]);

    const input: ReviewProtestInput = {
      protestId: 'protest-1',
      stewardId: 'steward-1',
      decision: 'uphold',
      decisionNotes: 'Notes',
    };

    const result = await useCase.execute(input);

    expect(result.isErr()).toBe(true);
    const error = result.unwrapErr() as ApplicationErrorCode<ReviewProtestErrorCode, { message: string }>;
    expect(error).toEqual({
      code: 'NOT_LEAGUE_ADMIN',
      details: { message: 'Only league owners and admins can review protests' },
    });
    expect(output.present).not.toHaveBeenCalled();
  });

  it('should uphold protest successfully', async () => {
    const mockProtest = { id: 'protest-1', raceId: 'race-1', uphold: vi.fn().mockReturnValue({}), dismiss: vi.fn() };
    const mockRace = { leagueId: 'league-1' };
    const memberships = [{ driverId: 'steward-1', status: 'active', role: 'admin' }];
    protestRepository.findById.mockResolvedValue(mockProtest);
    raceRepository.findById.mockResolvedValue(mockRace);
    leagueMembershipRepository.getLeagueMembers.mockResolvedValue(memberships);
    protestRepository.update.mockResolvedValue(undefined);

    const input: ReviewProtestInput = {
      protestId: 'protest-1',
      stewardId: 'steward-1',
      decision: 'uphold',
      decisionNotes: 'Notes',
    };

    const result = await useCase.execute(input);

    expect(result.isOk()).toBe(true);
    expect(result.unwrap()).toBeUndefined();
    expect(protestRepository.update).toHaveBeenCalledWith(mockProtest.uphold());
    expect(output.present).toHaveBeenCalledTimes(1);
    const presented = output.present.mock.calls[0]![0] as ReviewProtestResult;
    expect(presented).toEqual({
      leagueId: 'league-1',
      protestId: 'protest-1',
      status: 'upheld',
    });
  });

  it('should dismiss protest successfully', async () => {
    const mockProtest = { id: 'protest-1', raceId: 'race-1', uphold: vi.fn(), dismiss: vi.fn().mockReturnValue({}) };
    const mockRace = { leagueId: 'league-1' };
    const memberships = [{ driverId: 'steward-1', status: 'active', role: 'owner' }];
    protestRepository.findById.mockResolvedValue(mockProtest);
    raceRepository.findById.mockResolvedValue(mockRace);
    leagueMembershipRepository.getLeagueMembers.mockResolvedValue(memberships);
    protestRepository.update.mockResolvedValue(undefined);

    const input: ReviewProtestInput = {
      protestId: 'protest-1',
      stewardId: 'steward-1',
      decision: 'dismiss',
      decisionNotes: 'Notes',
    };

    const result = await useCase.execute(input);

    expect(result.isOk()).toBe(true);
    expect(result.unwrap()).toBeUndefined();
    expect(protestRepository.update).toHaveBeenCalledWith(mockProtest.dismiss());
    expect(output.present).toHaveBeenCalledTimes(1);
    const presented = output.present.mock.calls[0]![0] as ReviewProtestResult;
    expect(presented).toEqual({
      leagueId: 'league-1',
      protestId: 'protest-1',
      status: 'dismissed',
    });
  });

  it('should return repository error when update throws', async () => {
    const mockProtest = { id: 'protest-1', raceId: 'race-1', uphold: vi.fn().mockReturnValue({}), dismiss: vi.fn() };
    const mockRace = { leagueId: 'league-1' };
    const memberships = [{ driverId: 'steward-1', status: 'active', role: 'admin' }];
    protestRepository.findById.mockResolvedValue(mockProtest);
    raceRepository.findById.mockResolvedValue(mockRace);
    leagueMembershipRepository.getLeagueMembers.mockResolvedValue(memberships);
    protestRepository.update.mockRejectedValue(new Error('DB error'));

    const input: ReviewProtestInput = {
      protestId: 'protest-1',
      stewardId: 'steward-1',
      decision: 'uphold',
      decisionNotes: 'Notes',
    };

    const result = await useCase.execute(input);

    expect(result.isErr()).toBe(true);
    const error = result.unwrapErr() as ApplicationErrorCode<ReviewProtestErrorCode, { message: string }>;
    expect(error.code).toBe('REPOSITORY_ERROR');
    expect(error.details?.message).toBe('DB error');
    expect(output.present).not.toHaveBeenCalled();
  });
});