import type { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';

export interface RemoveLeagueMemberUseCaseParams {
  leagueId: string;
  targetDriverId: string;
}

export interface RemoveLeagueMemberResultDTO {
  success: boolean;
}

export class RemoveLeagueMemberUseCase {
  constructor(private readonly leagueMembershipRepository: ILeagueMembershipRepository) {}

  async execute(params: RemoveLeagueMemberUseCaseParams): Promise<Result<RemoveLeagueMemberResultDTO, ApplicationErrorCode<'MEMBERSHIP_NOT_FOUND'>>> {
    const memberships = await this.leagueMembershipRepository.getLeagueMembers(params.leagueId);
    const membership = memberships.find(m => m.driverId === params.targetDriverId);
    if (!membership) {
      return Result.err({ code: 'MEMBERSHIP_NOT_FOUND' });
    }
    await this.leagueMembershipRepository.saveMembership({
      ...membership,
      status: 'inactive',
    });
    return Result.ok({ success: true });
  }
}