import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
import type { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';
import type { AsyncUseCase } from '@core/shared/application';
import { Result } from '@core/shared/application/Result';
import type { GetLeagueAdminPermissionsResultDTO } from '../dto/GetLeagueAdminPermissionsResultDTO';

export class GetLeagueAdminPermissionsUseCase implements AsyncUseCase<{ leagueId: string; performerDriverId: string }, GetLeagueAdminPermissionsResultDTO, 'NO_ERROR'> {
  constructor(
    private readonly leagueRepository: ILeagueRepository,
    private readonly leagueMembershipRepository: ILeagueMembershipRepository,
  ) {}

  async execute(params: { leagueId: string; performerDriverId: string }): Promise<Result<GetLeagueAdminPermissionsResultDTO, never>> {
    const league = await this.leagueRepository.findById(params.leagueId);
    if (!league) {
      return Result.ok({ canRemoveMember: false, canUpdateRoles: false });
    }

    const membership = await this.leagueMembershipRepository.getMembership(params.leagueId, params.performerDriverId);
    if (!membership || membership.status !== 'active') {
      return Result.ok({ canRemoveMember: false, canUpdateRoles: false });
    }

    // Business logic: owners and admins can remove members and update roles
    const canRemoveMember = membership.role === 'owner' || membership.role === 'admin';
    const canUpdateRoles = membership.role === 'owner' || membership.role === 'admin';

    return Result.ok({ canRemoveMember, canUpdateRoles });
  }
}