import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import { Logger } from 'vite';
import { beforeEach, describe, expect, it, Mock, vi } from 'vitest';
import type { LeagueMembershipRepository } from '../../domain/repositories/LeagueMembershipRepository';
import type { ProtestRepository } from '../../domain/repositories/ProtestRepository';
import type { RaceRepository } from '../../domain/repositories/RaceRepository';
import { ReviewProtestUseCase, type ReviewProtestErrorCode, type ReviewProtestInput } from './ReviewProtestUseCase';

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 };
  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(),
    };
    useCase = new ReviewProtestUseCase(protestRepository as unknown as ProtestRepository,
      raceRepository as unknown as RaceRepository,
      leagueMembershipRepository as unknown as LeagueMembershipRepository,
      logger as unknown as Logger);
  });

  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' },
    });
    });

  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' },
    });
    });

  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' },
    });
    });

  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);
    const value = result.unwrap();
    expect(value).toEqual({
      leagueId: 'league-1',
      protestId: 'protest-1',
      status: 'upheld',
    });
    expect(protestRepository.update).toHaveBeenCalledWith(mockProtest.uphold());
  });

  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);
    const value = result.unwrap();
    expect(value).toEqual({
      leagueId: 'league-1',
      protestId: 'protest-1',
      status: 'dismissed',
    });
    expect(protestRepository.update).toHaveBeenCalledWith(mockProtest.dismiss());
  });

  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');
    });
});