import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
import { RemoveLeagueMemberUseCase } from './RemoveLeagueMemberUseCase';
import type { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';

describe('RemoveLeagueMemberUseCase', () => {
  let useCase: RemoveLeagueMemberUseCase;
  let leagueMembershipRepository: { getLeagueMembers: Mock; saveMembership: Mock };

  beforeEach(() => {
    leagueMembershipRepository = {
      getLeagueMembers: vi.fn(),
      saveMembership: vi.fn(),
    };
    useCase = new RemoveLeagueMemberUseCase(
      leagueMembershipRepository as unknown as ILeagueMembershipRepository,
    );
  });

  it('should remove league member by setting status to inactive', async () => {
    const leagueId = 'league-1';
    const targetDriverId = 'driver-1';
    const memberships = [{ leagueId, driverId: targetDriverId, role: 'member', status: 'active', joinedAt: new Date() }];

    leagueMembershipRepository.getLeagueMembers.mockResolvedValue(memberships);

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

    expect(result.isOk()).toBe(true);
    expect(result.unwrap()).toEqual({ success: true });
    expect(leagueMembershipRepository.saveMembership).toHaveBeenCalledWith({
      leagueId,
      driverId: targetDriverId,
      role: 'member',
      status: 'inactive',
      joinedAt: expect.any(Date),
    });
  });

  it('should return error if membership not found', async () => {
    leagueMembershipRepository.getLeagueMembers.mockResolvedValue([]);

    const result = await useCase.execute({ leagueId: 'league-1', targetDriverId: 'driver-1' });

    expect(result.isErr()).toBe(true);
    expect(result.unwrapErr()).toEqual({ code: 'MEMBERSHIP_NOT_FOUND' });
  });
});