import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import { beforeEach, describe, expect, it, vi, type Mock } from 'vitest';

import { Race } from '../../domain/entities/Race';
import { Season } from '../../domain/entities/season/Season';
import type { RaceRepository } from '../../domain/repositories/RaceRepository';
import type { SeasonRepository } from '../../domain/repositories/SeasonRepository';

import { Logger } from 'vite';
import {
    DeleteLeagueSeasonScheduleRaceUseCase,
    type DeleteLeagueSeasonScheduleRaceErrorCode,
} from './DeleteLeagueSeasonScheduleRaceUseCase';

function createLogger(): Logger {
  return {
    debug: vi.fn(),
    info: vi.fn(),
    warn: vi.fn(),
    error: vi.fn(),
  } as unknown as Logger;
}

function createSeasonWithinWindow(overrides?: Partial<{ leagueId: string }>): Season {
  return Season.create({
    id: 'season-1',
    leagueId: overrides?.leagueId ?? 'league-1',
    gameId: 'iracing',
    name: 'Schedule Season',
    status: 'planned',
    startDate: new Date('2025-01-01T00:00:00Z'),
    endDate: new Date('2025-01-31T00:00:00Z'),
  });
}

describe('DeleteLeagueSeasonScheduleRaceUseCase', () => {
  let seasonRepository: { findById: Mock };
  let raceRepository: { findById: Mock; delete: Mock };
  let logger: Logger;

  beforeEach(() => {
    seasonRepository = { findById: vi.fn() };
    raceRepository = { findById: vi.fn(), delete: vi.fn() };
    logger = createLogger();
  });

  it('deletes race when season belongs to league and race belongs to league', async () => {
    const season = createSeasonWithinWindow();
    seasonRepository.findById.mockResolvedValue(season);

    const existing = Race.create({
      id: 'race-1',
      leagueId: 'league-1',
      track: 'Track',
      car: 'Car',
      scheduledAt: new Date('2025-01-05T20:00:00Z'),
    });
    raceRepository.findById.mockResolvedValue(existing);
    raceRepository.delete.mockResolvedValue(undefined);

    const useCase = new DeleteLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as SeasonRepository,
      raceRepository as unknown as RaceRepository,
      logger);

    const result = await useCase.execute({
      leagueId: 'league-1',
      seasonId: 'season-1',
      raceId: 'race-1',
    });

    expect(result.isOk()).toBe(true);
    expect(raceRepository.delete).toHaveBeenCalledTimes(1);
    expect(raceRepository.delete).toHaveBeenCalledWith('race-1');
  });

  it('returns SEASON_NOT_FOUND when season does not belong to league and does not read/delete race', async () => {
    const season = createSeasonWithinWindow({ leagueId: 'other-league' });
    seasonRepository.findById.mockResolvedValue(season);

    const useCase = new DeleteLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as SeasonRepository,
      raceRepository as unknown as RaceRepository,
      logger);

    const result = await useCase.execute({
      leagueId: 'league-1',
      seasonId: 'season-1',
      raceId: 'race-1',
    });

    expect(result.isErr()).toBe(true);
    const error = result.unwrapErr() as ApplicationErrorCode<
      DeleteLeagueSeasonScheduleRaceErrorCode,
      { message: string }
    >;
    expect(error.code).toBe('SEASON_NOT_FOUND');
    expect(raceRepository.findById).not.toHaveBeenCalled();
    expect(raceRepository.delete).not.toHaveBeenCalled();
  });

  it('returns RACE_NOT_FOUND when race does not exist for league and does not delete', async () => {
    const season = createSeasonWithinWindow();
    seasonRepository.findById.mockResolvedValue(season);
    raceRepository.findById.mockResolvedValue(null);

    const useCase = new DeleteLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as SeasonRepository,
      raceRepository as unknown as RaceRepository,
      logger);

    const result = await useCase.execute({
      leagueId: 'league-1',
      seasonId: 'season-1',
      raceId: 'race-404',
    });

    expect(result.isErr()).toBe(true);
    const error = result.unwrapErr() as ApplicationErrorCode<
      DeleteLeagueSeasonScheduleRaceErrorCode,
      { message: string }
    >;
    expect(error.code).toBe('RACE_NOT_FOUND');
    expect(raceRepository.delete).not.toHaveBeenCalled();
  });

  it('returns RACE_NOT_FOUND when race belongs to different league', async () => {
    const season = createSeasonWithinWindow();
    seasonRepository.findById.mockResolvedValue(season);

    const existing = Race.create({
      id: 'race-1',
      leagueId: 'other-league',
      track: 'Track',
      car: 'Car',
      scheduledAt: new Date('2025-01-05T20:00:00Z'),
    });
    raceRepository.findById.mockResolvedValue(existing);

    const useCase = new DeleteLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as SeasonRepository,
      raceRepository as unknown as RaceRepository,
      logger);

    const result = await useCase.execute({
      leagueId: 'league-1',
      seasonId: 'season-1',
      raceId: 'race-1',
    });

    expect(result.isErr()).toBe(true);
    const error = result.unwrapErr() as ApplicationErrorCode<
      DeleteLeagueSeasonScheduleRaceErrorCode,
      { message: string }
    >;
    expect(error.code).toBe('RACE_NOT_FOUND');
    expect(raceRepository.delete).not.toHaveBeenCalled();
  });

  it('returns SEASON_NOT_FOUND when season does not exist', async () => {
    seasonRepository.findById.mockResolvedValue(null);

    const useCase = new DeleteLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as SeasonRepository,
      raceRepository as unknown as RaceRepository,
      logger);

    const result = await useCase.execute({
      leagueId: 'league-1',
      seasonId: 'season-1',
      raceId: 'race-1',
    });

    expect(result.isErr()).toBe(true);
    const error = result.unwrapErr() as ApplicationErrorCode<
      DeleteLeagueSeasonScheduleRaceErrorCode,
      { message: string }
    >;
    expect(error.code).toBe('SEASON_NOT_FOUND');
    expect(raceRepository.findById).not.toHaveBeenCalled();
    expect(raceRepository.delete).not.toHaveBeenCalled();
  });

  it('returns REPOSITORY_ERROR when repository throws during find', async () => {
    const repositoryError = new Error('DB connection failed');
    seasonRepository.findById.mockRejectedValue(repositoryError);

    const useCase = new DeleteLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as SeasonRepository,
      raceRepository as unknown as RaceRepository,
      logger);

    const result = await useCase.execute({
      leagueId: 'league-1',
      seasonId: 'season-1',
      raceId: 'race-1',
    });

    expect(result.isErr()).toBe(true);
    const error = result.unwrapErr() as ApplicationErrorCode<
      DeleteLeagueSeasonScheduleRaceErrorCode,
      { message: string }
    >;
    expect(error.code).toBe('REPOSITORY_ERROR');
    expect(error.details.message).toBe('DB connection failed');
  });

  it('returns REPOSITORY_ERROR when repository throws during delete', async () => {
    const season = createSeasonWithinWindow();
    const existing = Race.create({
      id: 'race-1',
      leagueId: 'league-1',
      track: 'Track',
      car: 'Car',
      scheduledAt: new Date('2025-01-05T20:00:00Z'),
    });
    const repositoryError = new Error('DB delete failed');
    seasonRepository.findById.mockResolvedValue(season);
    raceRepository.findById.mockResolvedValue(existing);
    raceRepository.delete.mockRejectedValue(repositoryError);

    const useCase = new DeleteLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as SeasonRepository,
      raceRepository as unknown as RaceRepository,
      logger);

    const result = await useCase.execute({
      leagueId: 'league-1',
      seasonId: 'season-1',
      raceId: 'race-1',
    });

    expect(result.isErr()).toBe(true);
    const error = result.unwrapErr() as ApplicationErrorCode<
      DeleteLeagueSeasonScheduleRaceErrorCode,
      { message: string }
    >;
    expect(error.code).toBe('REPOSITORY_ERROR');
    expect(error.details.message).toBe('DB delete failed');
  });

  it('returns REPOSITORY_ERROR when repository throws during race find', async () => {
    const season = createSeasonWithinWindow();
    const repositoryError = new Error('DB connection failed');
    seasonRepository.findById.mockResolvedValue(season);
    raceRepository.findById.mockRejectedValue(repositoryError);

    const useCase = new DeleteLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as SeasonRepository,
      raceRepository as unknown as RaceRepository,
      logger);

    const result = await useCase.execute({
      leagueId: 'league-1',
      seasonId: 'season-1',
      raceId: 'race-1',
    });

    expect(result.isErr()).toBe(true);
    const error = result.unwrapErr() as ApplicationErrorCode<
      DeleteLeagueSeasonScheduleRaceErrorCode,
      { message: string }
    >;
    expect(error.code).toBe('REPOSITORY_ERROR');
    expect(error.details.message).toBe('DB connection failed');
    expect(raceRepository.delete).not.toHaveBeenCalled();
  });
});