import type { Logger } from '@core/shared/domain/Logger';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import { beforeEach, describe, expect, it, Mock, vi } from 'vitest';
import {
    PreviewLeagueScheduleUseCase,
    type PreviewLeagueScheduleErrorCode,
    type PreviewLeagueScheduleInput
} from './PreviewLeagueScheduleUseCase';

describe('PreviewLeagueScheduleUseCase', () => {
  let useCase: PreviewLeagueScheduleUseCase;
  let logger: {
    debug: Mock;
    info: Mock;
    warn: Mock;
    error: Mock;
  };

  beforeEach(() => {
    logger = {
      debug: vi.fn(),
      info: vi.fn(),
      warn: vi.fn(),
      error: vi.fn(),
    };
    useCase = new PreviewLeagueScheduleUseCase(undefined, logger as unknown as Logger);
  });

  it('should preview schedule successfully', async () => {
    const params: PreviewLeagueScheduleInput = {
      schedule: {
        seasonStartDate: '2024-01-01',
        recurrenceStrategy: 'weekly',
        weekdays: ['Mon'],
        raceStartTime: '20:00',
        timezoneId: 'UTC',
        plannedRounds: 5,
      },
      maxRounds: 3,
    };

    const result = await useCase.execute(params);

    expect(result.isOk()).toBe(true);
    const scheduleResult = result.unwrap();
    expect(scheduleResult.rounds).toBeDefined();
    expect(scheduleResult.rounds.length).toBeGreaterThan(0);
    expect(scheduleResult.summary).toContain('Every Mon');
  });

  it('should return error for invalid schedule', async () => {
    const params: PreviewLeagueScheduleInput = {
      schedule: {
        seasonStartDate: 'invalid',
        recurrenceStrategy: 'weekly',
        weekdays: ['Mon'],
        raceStartTime: '20:00',
        timezoneId: 'UTC',
        plannedRounds: 5,
      },
    };

    const result = await useCase.execute(params);

    expect(result.isErr()).toBe(true);
    const err = result.unwrapErr() as ApplicationErrorCode<
      PreviewLeagueScheduleErrorCode,
      { message: string }
    >;
    expect(err.code).toBe('INVALID_SCHEDULE');
    expect(err.details.message).toBe('Invalid schedule data');
  });

  it('should return REPOSITORY_ERROR when generator throws', async () => {
    const throwingGenerator = {
      generateSlotsUpTo: vi.fn(() => {
        throw new Error('boom');
      }),
    } as unknown as Pick<
      typeof import('../../domain/services/SeasonScheduleGenerator').SeasonScheduleGenerator,
      'generateSlotsUpTo'
    >;

    const throwingUseCase = new PreviewLeagueScheduleUseCase(throwingGenerator,
      logger as unknown as Logger);

    const params: PreviewLeagueScheduleInput = {
      schedule: {
        seasonStartDate: '2024-01-01',
        recurrenceStrategy: 'weekly',
        weekdays: ['Mon'],
        raceStartTime: '20:00',
        timezoneId: 'UTC',
        plannedRounds: 5,
      },
      maxRounds: 3,
    };

    const result = await throwingUseCase.execute(params);

    expect(result.isErr()).toBe(true);
    const err = result.unwrapErr() as ApplicationErrorCode<
      PreviewLeagueScheduleErrorCode,
      { message: string }
    >;
    expect(err.code).toBe('REPOSITORY_ERROR');
    expect(err.details.message).toBe('boom');
  });
});
