import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
import { QuickPenaltyUseCase } from './QuickPenaltyUseCase';
import type { IPenaltyRepository } from '../../domain/repositories/IPenaltyRepository';
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
import type { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';
import type { Logger } from '@core/shared/application';

describe('QuickPenaltyUseCase', () => {
  let useCase: QuickPenaltyUseCase;
  let penaltyRepository: {
    create: Mock;
  };
  let raceRepository: {
    findById: Mock;
  };
  let leagueMembershipRepository: {
    getLeagueMembers: Mock;
  };
  let logger: {
    debug: Mock;
    info: Mock;
    warn: Mock;
    error: Mock;
  };

  beforeEach(() => {
    penaltyRepository = {
      create: 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 QuickPenaltyUseCase(
      penaltyRepository as unknown as IPenaltyRepository,
      raceRepository as unknown as IRaceRepository,
      leagueMembershipRepository as unknown as ILeagueMembershipRepository,
      logger as unknown as Logger,
    );
  });

  it('should apply penalty successfully', async () => {
    const command = {
      raceId: 'race-1',
      driverId: 'driver-1',
      adminId: 'admin-1',
      infractionType: 'track_limits' as const,
      severity: 'minor' as const,
      notes: 'Test penalty',
    };

    raceRepository.findById.mockResolvedValue({ id: 'race-1', leagueId: 'league-1' });
    leagueMembershipRepository.getLeagueMembers.mockResolvedValue([
      { driverId: 'admin-1', role: 'admin', status: 'active' },
    ]);
    penaltyRepository.create.mockResolvedValue(undefined);

    const result = await useCase.execute(command);

    expect(result.isOk()).toBe(true);
    expect(result.unwrap()).toHaveProperty('penaltyId');
  });

  it('should return error when race not found', async () => {
    const command = {
      raceId: 'race-1',
      driverId: 'driver-1',
      adminId: 'admin-1',
      infractionType: 'track_limits' as const,
      severity: 'minor' as const,
    };

    raceRepository.findById.mockResolvedValue(null);

    const result = await useCase.execute(command);

    expect(result.isErr()).toBe(true);
    expect(result.unwrapErr()).toEqual({
      code: 'RACE_NOT_FOUND',
      details: { message: 'Race not found' },
    });
  });

  it('should return error when admin unauthorized', async () => {
    const command = {
      raceId: 'race-1',
      driverId: 'driver-1',
      adminId: 'admin-1',
      infractionType: 'track_limits' as const,
      severity: 'minor' as const,
    };

    raceRepository.findById.mockResolvedValue({ id: 'race-1', leagueId: 'league-1' });
    leagueMembershipRepository.getLeagueMembers.mockResolvedValue([
      { driverId: 'admin-1', role: 'member', status: 'active' },
    ]);

    const result = await useCase.execute(command);

    expect(result.isErr()).toBe(true);
    expect(result.unwrapErr()).toEqual({
      code: 'UNAUTHORIZED',
      details: { message: 'Only league owners and admins can issue penalties' },
    });
  });

  it('should handle other infraction type', async () => {
    const command = {
      raceId: 'race-1',
      driverId: 'driver-1',
      adminId: 'admin-1',
      infractionType: 'other' as const,
      severity: 'major' as const,
    };

    raceRepository.findById.mockResolvedValue({ id: 'race-1', leagueId: 'league-1' });
    leagueMembershipRepository.getLeagueMembers.mockResolvedValue([
      { driverId: 'admin-1', role: 'admin', status: 'active' },
    ]);
    penaltyRepository.create.mockResolvedValue(undefined);

    const result = await useCase.execute(command);

    expect(result.isOk()).toBe(true);
  });
});