import type { LeaguesApiClient } from '../../api/leagues/LeaguesApiClient';
import type { LeagueSummaryPresenter } from '../../presenters/LeagueSummaryPresenter';
import type { LeagueStandingsPresenter } from '../../presenters/LeagueStandingsPresenter';
import type { LeagueSummaryViewModel, LeagueStandingsViewModel } from '../../view-models';
import type { CreateLeagueInputDto, CreateLeagueOutputDto, LeagueStatsDto, LeagueScheduleDto, LeagueMembershipsDto } from '../../dtos';

/**
 * League Service
 *
 * Orchestrates league operations by coordinating API calls and presentation logic.
 * All dependencies are injected via constructor.
 */
export class LeagueService {
  constructor(
    private readonly apiClient: LeaguesApiClient,
    private readonly leagueSummaryPresenter: LeagueSummaryPresenter,
    private readonly leagueStandingsPresenter: LeagueStandingsPresenter
  ) {}

  /**
   * Get all leagues with presentation transformation
   */
  async getAllLeagues(): Promise<LeagueSummaryViewModel[]> {
    const dto = await this.apiClient.getAllWithCapacity();
    return this.leagueSummaryPresenter.present(dto);
  }

  /**
   * Get league standings with presentation transformation
   */
  async getLeagueStandings(leagueId: string, currentUserId: string): Promise<LeagueStandingsViewModel> {
    const dto = await this.apiClient.getStandings(leagueId);
    // TODO: include drivers and memberships in dto
    const dtoWithExtras = {
      ...dto,
      drivers: [], // TODO: fetch drivers
      memberships: [], // TODO: fetch memberships
    };
    return this.leagueStandingsPresenter.present(dtoWithExtras, currentUserId);
  }

  /**
   * Get league statistics
   */
  async getLeagueStats(): Promise<LeagueStatsDto> {
    return await this.apiClient.getTotal();
  }

  /**
   * Get league schedule
   */
  async getLeagueSchedule(leagueId: string): Promise<LeagueScheduleDto> {
    return await this.apiClient.getSchedule(leagueId);
  }

  /**
   * Get league memberships
   */
  async getLeagueMemberships(leagueId: string): Promise<LeagueMembershipsDto> {
    return await this.apiClient.getMemberships(leagueId);
  }

  /**
   * Create a new league
   */
  async createLeague(input: CreateLeagueInputDto): Promise<CreateLeagueOutputDto> {
    return await this.apiClient.create(input);
  }

  /**
   * Remove a member from league
   */
  async removeMember(leagueId: string, performerDriverId: string, targetDriverId: string): Promise<{ success: boolean }> {
    return await this.apiClient.removeMember(leagueId, performerDriverId, targetDriverId);
  }
}