import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
import { GetLeagueJoinRequestsUseCase } from './GetLeagueJoinRequestsUseCase';
import { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';
import { IDriverRepository } from '../../domain/repositories/IDriverRepository';
import { Driver } from '../../domain/entities/Driver';

describe('GetLeagueJoinRequestsUseCase', () => {
  let useCase: GetLeagueJoinRequestsUseCase;
  let leagueMembershipRepository: {
    getJoinRequests: Mock;
  };
  let driverRepository: {
    findById: Mock;
  };

  beforeEach(() => {
    leagueMembershipRepository = {
      getJoinRequests: vi.fn(),
    };
    driverRepository = {
      findById: vi.fn(),
    };
    useCase = new GetLeagueJoinRequestsUseCase(
      leagueMembershipRepository as unknown as ILeagueMembershipRepository,
      driverRepository as unknown as IDriverRepository,
    );
  });

  it('should return join requests with drivers', async () => {
    const leagueId = 'league-1';
    const joinRequests = [
      { id: 'req-1', leagueId, driverId: 'driver-1', requestedAt: new Date(), message: 'msg' },
    ];
    const driver = Driver.create({
      id: 'driver-1',
      iracingId: '123',
      name: 'Driver 1',
      country: 'US',
    });

    leagueMembershipRepository.getJoinRequests.mockResolvedValue(joinRequests);
    driverRepository.findById.mockResolvedValue(driver);

    const result = await useCase.execute({ leagueId });

    expect(result.isOk()).toBe(true);
    expect(result.unwrap()).toEqual({
      joinRequests: [
        {
          id: 'req-1',
          leagueId,
          driverId: 'driver-1',
          requestedAt: expect.any(Date),
          message: 'msg',
          driver: { id: 'driver-1', name: 'Driver 1' },
        },
      ],
    });
  });
});