import { ForbiddenException } from '@nestjs/common';
import { getActorFromRequestContext } from '../auth/getActorFromRequestContext';
import type { GetLeagueAdminPermissionsUseCase } from '@core/racing/application/use-cases/GetLeagueAdminPermissionsUseCase';

type GetLeagueAdminPermissionsUseCaseLike = Pick<GetLeagueAdminPermissionsUseCase, 'execute'>;

export async function requireLeagueAdminOrOwner(
  leagueId: string,
  getLeagueAdminPermissionsUseCase: GetLeagueAdminPermissionsUseCaseLike,
): Promise<void> {
  const actor = getActorFromRequestContext();

  const permissionResult = await getLeagueAdminPermissionsUseCase.execute({
    leagueId,
    performerDriverId: actor.driverId,
  });

  if (permissionResult.isErr()) {
    throw new ForbiddenException('Forbidden');
  }
}