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

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

import { Logger } from 'vite';
import {
    UnpublishLeagueSeasonScheduleUseCase,
    type UnpublishLeagueSeasonScheduleErrorCode,
} from './UnpublishLeagueSeasonScheduleUseCase';

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('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 updatedSeason = seasonRepository.update.mock.calls[0]?.[0] as Season;
    expect(updatedSeason.id).toBe('season-1');
    expect(updatedSeason.leagueId).toBe('league-1');
    expect(updatedSeason.schedulePublished).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' }).withSchedulePublished(true);
    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();
  });

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

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

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

    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('REPOSITORY_ERROR');
    expect(error.details.message).toBe('DB connection failed');
  });

  it('returns REPOSITORY_ERROR when repository throws during update', async () => {
    const season = createSeasonWithinWindow().withSchedulePublished(true);
    const repositoryError = new Error('DB write failed');
    seasonRepository.findById.mockResolvedValue(season);
    seasonRepository.update.mockRejectedValue(repositoryError);

    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('REPOSITORY_ERROR');
    expect(error.details.message).toBe('DB write failed');
  });
});