import { beforeEach, describe, expect, it, vi } from 'vitest';
import type { Logger } from '@core/shared/application';
import { InMemoryLeagueMembershipRepository } from './InMemoryLeagueMembershipRepository';

describe('InMemoryLeagueMembershipRepository', () => {
  let repository: InMemoryLeagueMembershipRepository;
  let logger: Logger;

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

    repository = new InMemoryLeagueMembershipRepository(logger);
  });

  it('saves and queries memberships and join requests', async () => {
    const membership: { id: string; leagueId: string; driverId: string; status: string } = {
      id: 'm1',
      leagueId: 'league-1',
      driverId: 'driver-1',
      status: 'active',
    };

    await repository.saveMembership(
      membership as unknown as Parameters<InMemoryLeagueMembershipRepository['saveMembership']>[0],
    );

    expect((await repository.getMembership('league-1', 'driver-1'))?.id).toBe('m1');
    expect((await repository.findActiveByLeagueIdAndDriverId('league-1', 'driver-1'))?.id).toBe('m1');

    expect((await repository.findAllByLeagueId('league-1')).length).toBe(1);
    expect((await repository.findAllByDriverId('driver-1')).length).toBe(1);
    expect((await repository.getLeagueMembers('league-1')).length).toBe(1);

    const joinRequest: { id: string; leagueId: string } = { id: 'jr1', leagueId: 'league-1' };
    await repository.saveJoinRequest(
      joinRequest as unknown as Parameters<InMemoryLeagueMembershipRepository['saveJoinRequest']>[0],
    );

    expect((await repository.getJoinRequests('league-1')).map(r => r.id)).toEqual(['jr1']);

    await repository.removeJoinRequest('jr1');
    expect((await repository.getJoinRequests('league-1')).length).toBe(0);

    await repository.removeMembership('league-1', 'driver-1');
    expect(await repository.getMembership('league-1', 'driver-1')).toBeNull();
  });
});
