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

export type GetLeagueAdminInput = {
  leagueId: string;
};

export type GetLeagueAdminResult = {
  league: League;
};

export type GetLeagueAdminErrorCode = 'LEAGUE_NOT_FOUND' | 'REPOSITORY_ERROR';

export class GetLeagueAdminUseCase {
  constructor(
    private readonly leagueRepository: LeagueRepository,
  ) {}

  async execute(
    input: GetLeagueAdminInput,
  ): Promise<Result<GetLeagueAdminResult, ApplicationErrorCode<GetLeagueAdminErrorCode, { message: string }>>> {
    try {
      const league = await this.leagueRepository.findById(input.leagueId);
      if (!league) {
        return Result.err({ code: 'LEAGUE_NOT_FOUND', details: { message: 'League not found' } });
      }

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