import { beforeEach, describe, expect, it, vi, type Mock } from 'vitest';

import type { Logger } from '@core/shared/domain/Logger';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';

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 {
    CreateLeagueSeasonScheduleRaceUseCase,
    type CreateLeagueSeasonScheduleRaceErrorCode
} from './CreateLeagueSeasonScheduleRaceUseCase';
import {
    DeleteLeagueSeasonScheduleRaceUseCase,
    type DeleteLeagueSeasonScheduleRaceErrorCode
} from './DeleteLeagueSeasonScheduleRaceUseCase';
import {
    PublishLeagueSeasonScheduleUseCase,
    type PublishLeagueSeasonScheduleErrorCode
} from './PublishLeagueSeasonScheduleUseCase';
import {
    UnpublishLeagueSeasonScheduleUseCase,
    type UnpublishLeagueSeasonScheduleErrorCode
} from './UnpublishLeagueSeasonScheduleUseCase';
import {
    UpdateLeagueSeasonScheduleRaceUseCase,
    type UpdateLeagueSeasonScheduleRaceErrorCode
} from './UpdateLeagueSeasonScheduleRaceUseCase';

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('CreateLeagueSeasonScheduleRaceUseCase', () => {
  let seasonRepository: { findById: Mock };
  let raceRepository: { create: Mock };
  let logger: Logger;

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

  it('creates a race when season belongs to league and scheduledAt is within season window', async () => {
    const season = createSeasonWithinWindow();
    seasonRepository.findById.mockResolvedValue(season);
    raceRepository.create.mockImplementation(async (race: Race) => race);

    const useCase = new CreateLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as SeasonRepository,
      raceRepository as unknown as RaceRepository,
      logger,
      { generateRaceId: () => 'race-123' },
    );

    const scheduledAt = new Date('2025-01-10T20:00:00Z');
    const result = await useCase.execute({
      leagueId: 'league-1',
      seasonId: 'season-1',
      track: 'Road Atlanta',
      car: 'MX-5',
      scheduledAt,
    });

    expect(result.isOk()).toBe(true);
    expect(raceRepository.create).toHaveBeenCalledTimes(1);
    const createdRace = raceRepository.create.mock.calls[0]?.[0] as Race;
    expect(createdRace.id).toBe('race-123');
    expect(createdRace.leagueId).toBe('league-1');
    expect(createdRace.track).toBe('Road Atlanta');
    expect(createdRace.car).toBe('MX-5');
    expect(createdRace.scheduledAt.getTime()).toBe(scheduledAt.getTime());
  });

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

    const useCase = new CreateLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as SeasonRepository,
      raceRepository as unknown as RaceRepository,
      logger,
      { generateRaceId: () => 'race-123' },
    );

    const result = await useCase.execute({
      leagueId: 'league-1',
      seasonId: 'season-1',
      track: 'Road Atlanta',
      car: 'MX-5',
      scheduledAt: new Date('2025-01-10T20:00:00Z'),
    });

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

  it('returns RACE_OUTSIDE_SEASON_WINDOW when scheduledAt is before season start and does not create', async () => {
    const season = createSeasonWithinWindow();
    seasonRepository.findById.mockResolvedValue(season);

    const useCase = new CreateLeagueSeasonScheduleRaceUseCase(seasonRepository as unknown as SeasonRepository,
      raceRepository as unknown as RaceRepository,
      logger,
      { generateRaceId: () => 'race-123' },
    );

    const result = await useCase.execute({
      leagueId: 'league-1',
      seasonId: 'season-1',
      track: 'Road Atlanta',
      car: 'MX-5',
      scheduledAt: new Date('2024-12-31T23:59:59Z'),
    });

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

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

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

  it('updates race when season belongs to league and updated scheduledAt stays within window', async () => {
    const season = createSeasonWithinWindow();
    seasonRepository.findById.mockResolvedValue(season);

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

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

    const newScheduledAt = new Date('2025-01-20T20:00:00Z');
    const result = await useCase.execute({
      leagueId: 'league-1',
      seasonId: 'season-1',
      raceId: 'race-1',
      track: 'New Track',
      car: 'New Car',
      scheduledAt: newScheduledAt,
    });

    expect(result.isOk()).toBe(true);
    expect(raceRepository.update).toHaveBeenCalledTimes(1);
    const presented = result.unwrap();
    expect(presented.success).toBe(true);
  });

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

    const useCase = new UpdateLeagueSeasonScheduleRaceUseCase(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',
      track: 'New Track',
    });

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

  it('returns RACE_OUTSIDE_SEASON_WINDOW when updated scheduledAt is outside window and does not update', async () => {
    const season = createSeasonWithinWindow();
    seasonRepository.findById.mockResolvedValue(season);

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

    const useCase = new UpdateLeagueSeasonScheduleRaceUseCase(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',
      scheduledAt: new Date('2025-02-01T00:00:01Z'),
    });

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

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

    const useCase = new UpdateLeagueSeasonScheduleRaceUseCase(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',
      track: 'New Track',
    });

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

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

describe('PublishLeagueSeasonScheduleUseCase', () => {
  let seasonRepository: { findById: Mock; update: Mock };
  let logger: Logger;

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

  it('publishes schedule deterministically (schedulePublished=true) and persists', async () => {
    const season = createSeasonWithinWindow();
    seasonRepository.findById.mockResolvedValue(season);
    seasonRepository.update.mockResolvedValue(undefined);

    const useCase = new PublishLeagueSeasonScheduleUseCase(seasonRepository as unknown as SeasonRepository,
      logger);

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

    expect(result.isOk()).toBe(true);
    expect(seasonRepository.update).toHaveBeenCalledTimes(1);
    const presented = result.unwrap();
    expect(presented.seasonId).toBe('season-1');
    expect(presented.published).toBe(true);
  });

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

    const useCase = new PublishLeagueSeasonScheduleUseCase(seasonRepository as unknown as SeasonRepository,
      logger);

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

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

describe('UnpublishLeagueSeasonScheduleUseCase', () => {
  let seasonRepository: { findById: Mock; update: Mock };
  let logger: Logger;

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

  it('unpublishes schedule deterministically (schedulePublished=false) and persists', async () => {
    const season = createSeasonWithinWindow().withSchedulePublished(true);
    seasonRepository.findById.mockResolvedValue(season);
    seasonRepository.update.mockResolvedValue(undefined);

    const useCase = new UnpublishLeagueSeasonScheduleUseCase(seasonRepository as unknown as SeasonRepository,
      logger);

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

    expect(result.isOk()).toBe(true);
    expect(seasonRepository.update).toHaveBeenCalledTimes(1);
    const presented = result.unwrap();
    expect(presented.seasonId).toBe('season-1');
    expect(presented.published).toBe(false);
  });

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

    const useCase = new UnpublishLeagueSeasonScheduleUseCase(seasonRepository as unknown as SeasonRepository,
      logger);

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

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