import type { Logger } from '@core/shared/domain/Logger';
import { Result } from '@core/shared/domain/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type { League } from '../../domain/entities/League';
import { LeagueMembershipRepository } from '../../domain/repositories/LeagueMembershipRepository';
import { LeagueRepository } from '../../domain/repositories/LeagueRepository';

export interface GetLeagueAdminPermissionsInput {
  leagueId: string;
  performerDriverId: string;
}

export type LeagueAdminPermissions = {
  canManageSchedule: boolean;
  canManageMembers: boolean;
  canManageSponsorships: boolean;
  canManageScoring: boolean;
};

export type GetLeagueAdminPermissionsResult = {
  league: League;
  permissions: LeagueAdminPermissions;
};

export type GetLeagueAdminPermissionsErrorCode =
  | 'LEAGUE_NOT_FOUND'
  | 'USER_NOT_MEMBER'
  | 'REPOSITORY_ERROR';

export class GetLeagueAdminPermissionsUseCase {
  constructor(
    private readonly leagueRepository: LeagueRepository,
    private readonly leagueMembershipRepository: LeagueMembershipRepository,
    private readonly logger: Logger,
  ) {}

  async execute(
    input: GetLeagueAdminPermissionsInput,
  ): Promise<Result<GetLeagueAdminPermissionsResult, ApplicationErrorCode<GetLeagueAdminPermissionsErrorCode, { message: string }>>> {
    const { leagueId, performerDriverId } = input;

    try {
      const league = await this.leagueRepository.findById(leagueId);
      if (!league) {
        this.logger.warn('League not found when checking admin permissions', { leagueId, performerDriverId });
        return Result.err({
          code: 'LEAGUE_NOT_FOUND',
          details: { message: 'League not found' },
        });
      }

      const membership = await this.leagueMembershipRepository.getMembership(leagueId, performerDriverId);
      if (!membership || membership.status.toString() !== 'active' || (membership.role.toString() !== 'owner' && membership.role.toString() !== 'admin')) {
        this.logger.warn('User is not a member or not authorized for league admin permissions', {
          leagueId,
          performerDriverId,
        });
        return Result.err({
          code: 'USER_NOT_MEMBER',
          details: { message: 'User is not a member of this league' },
        });
      }

      const permissions: LeagueAdminPermissions = {
        canManageSchedule: true,
        canManageMembers: true,
        canManageSponsorships: true,
        canManageScoring: true,
      };

      const result: GetLeagueAdminPermissionsResult = {
        league,
        permissions,
      };

      return Result.ok(result);
    } catch (error) {
      const err = error instanceof Error ? error : new Error('Unknown error');
      this.logger.error('Failed to load league admin permissions', err);
      return Result.err({
        code: 'REPOSITORY_ERROR',
        details: { message: err.message ?? 'Failed to load league admin permissions' },
      });
    }
  }
}