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

describe('GetAllRacesUseCase', () => {
  const mockRaceFindAll = vi.fn();
  const mockRaceRepo: IRaceRepository = {
    findById: vi.fn(),
    findAll: mockRaceFindAll,
    findByLeagueId: vi.fn(),
    findUpcomingByLeagueId: vi.fn(),
    findCompletedByLeagueId: vi.fn(),
    findByStatus: vi.fn(),
    findByDateRange: vi.fn(),
    create: vi.fn(),
    update: vi.fn(),
    delete: vi.fn(),
    exists: vi.fn(),
  };

  const mockLeagueFindAll = vi.fn();
  const mockLeagueRepo: ILeagueRepository = {
    findById: vi.fn(),
    findAll: mockLeagueFindAll,
    findByOwnerId: vi.fn(),
    create: vi.fn(),
    update: vi.fn(),
    delete: vi.fn(),
    exists: vi.fn(),
    searchByName: vi.fn(),
  };

  const mockLogger: Logger = {
    debug: vi.fn(),
    info: vi.fn(),
    warn: vi.fn(),
    error: vi.fn(),
  };

  beforeEach(() => {
    vi.clearAllMocks();
  });

  it('should return races data', async () => {
    const useCase = new GetAllRacesUseCase(
      mockRaceRepo,
      mockLeagueRepo,
      mockLogger,
    );

    const race1 = {
      id: 'race1',
      track: 'Track A',
      car: 'Car A',
      scheduledAt: new Date('2023-01-01T10:00:00Z'),
      leagueId: 'league1',
    };
    const race2 = {
      id: 'race2',
      track: 'Track B',
      car: 'Car B',
      scheduledAt: new Date('2023-01-02T10:00:00Z'),
      leagueId: 'league2',
    };
    const league1 = { id: 'league1', name: 'League One' };
    const league2 = { id: 'league2', name: 'League Two' };

    mockRaceFindAll.mockResolvedValue([race1, race2]);
    mockLeagueFindAll.mockResolvedValue([league1, league2]);

    const result = await useCase.execute();

    expect(result.isOk()).toBe(true);
    expect(result.value).toEqual({
      races: [
        {
          id: 'race1',
          name: 'Track A - Car A',
          date: '2023-01-01T10:00:00.000Z',
          leagueName: 'League One',
        },
        {
          id: 'race2',
          name: 'Track B - Car B',
          date: '2023-01-02T10:00:00.000Z',
          leagueName: 'League Two',
        },
      ],
      totalCount: 2,
    });
  });

  it('should return empty result when no races or leagues', async () => {
    const useCase = new GetAllRacesUseCase(
      mockRaceRepo,
      mockLeagueRepo,
      mockLogger,
    );

    mockRaceFindAll.mockResolvedValue([]);
    mockLeagueFindAll.mockResolvedValue([]);

    const result = await useCase.execute();

    expect(result.isOk()).toBe(true);
    expect(result.value).toEqual({
      races: [],
      totalCount: 0,
    });
  });

  it('should return error when repository throws', async () => {
    const useCase = new GetAllRacesUseCase(
      mockRaceRepo,
      mockLeagueRepo,
      mockLogger,
    );

    const error = new Error('Repository error');
    mockRaceFindAll.mockRejectedValue(error);

    const result = await useCase.execute();

    expect(result.isErr()).toBe(true);
    expect(result.unwrapErr().code).toBe('REPOSITORY_ERROR');
    expect(result.unwrapErr().details.message).toBe('Repository error');
  });
});