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

describe('ReviewProtestUseCase', () => {
  let useCase: ReviewProtestUseCase;
  let protestRepository: { findById: Mock; update: Mock };
  let raceRepository: { findById: Mock };
  let leagueMembershipRepository: { getLeagueMembers: Mock };

  beforeEach(() => {
    protestRepository = { findById: vi.fn(), update: vi.fn() };
    raceRepository = { findById: vi.fn() };
    leagueMembershipRepository = { getLeagueMembers: vi.fn() };
    useCase = new ReviewProtestUseCase(
      protestRepository as unknown as IProtestRepository,
      raceRepository as unknown as IRaceRepository,
      leagueMembershipRepository 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',
      decision: 'uphold',
      decisionNotes: 'Notes',
    });

    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' };
    protestRepository.findById.mockResolvedValue(mockProtest);
    raceRepository.findById.mockResolvedValue(null);

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

    expect(result.isErr()).toBe(true);
    expect(result.unwrapErr()).toEqual({ code: '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 result = await useCase.execute({
      protestId: 'protest-1',
      stewardId: 'steward-1',
      decision: 'uphold',
      decisionNotes: 'Notes',
    });

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

  it('should uphold protest successfully', async () => {
    const mockProtest = { 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 result = await useCase.execute({
      protestId: 'protest-1',
      stewardId: 'steward-1',
      decision: 'uphold',
      decisionNotes: 'Notes',
    });

    expect(result.isOk()).toBe(true);
    expect(result.unwrap()).toBeUndefined();
    expect(protestRepository.update).toHaveBeenCalledWith(mockProtest.uphold());
  });

  it('should dismiss protest successfully', async () => {
    const mockProtest = { 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 result = await useCase.execute({
      protestId: 'protest-1',
      stewardId: 'steward-1',
      decision: 'dismiss',
      decisionNotes: 'Notes',
    });

    expect(result.isOk()).toBe(true);
    expect(result.unwrap()).toBeUndefined();
    expect(protestRepository.update).toHaveBeenCalledWith(mockProtest.dismiss());
  });
});