import { LeaguesApiClient } from "@/lib/api/leagues/LeaguesApiClient"; import { CreateLeagueInputDTO } from "@/lib/types/generated/CreateLeagueInputDTO"; import { LeagueWithCapacityDTO } from "@/lib/types/generated/LeagueWithCapacityDTO"; import { CreateLeagueViewModel } from "@/lib/view-models/CreateLeagueViewModel"; import { LeagueMembershipsViewModel } from "@/lib/view-models/LeagueMembershipsViewModel"; import { LeagueScheduleViewModel } from "@/lib/view-models/LeagueScheduleViewModel"; import { LeagueStandingsViewModel } from "@/lib/view-models/LeagueStandingsViewModel"; import { LeagueStatsViewModel } from "@/lib/view-models/LeagueStatsViewModel"; import { LeagueSummaryViewModel } from "@/lib/view-models/LeagueSummaryViewModel"; import { RemoveMemberViewModel } from "@/lib/view-models/RemoveMemberViewModel"; /** * League Service * * Orchestrates league operations by coordinating API calls and view model creation. * All dependencies are injected via constructor. */ export class LeagueService { constructor( private readonly apiClient: LeaguesApiClient ) {} /** * Get all leagues with view model transformation */ async getAllLeagues(): Promise { const dto = await this.apiClient.getAllWithCapacity(); return dto.leagues.map((league: LeagueWithCapacityDTO) => new LeagueSummaryViewModel(league)); } /** * Get league standings with view model transformation */ async getLeagueStandings(leagueId: string, currentUserId: string): Promise { 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 new LeagueStandingsViewModel(dtoWithExtras, currentUserId); } /** * Get league statistics */ async getLeagueStats(): Promise { const dto = await this.apiClient.getTotal(); return new LeagueStatsViewModel(dto); } /** * Get league schedule */ async getLeagueSchedule(leagueId: string): Promise { const dto = await this.apiClient.getSchedule(leagueId); return new LeagueScheduleViewModel(dto); } /** * Get league memberships */ async getLeagueMemberships(leagueId: string, currentUserId: string): Promise { const dto = await this.apiClient.getMemberships(leagueId); return new LeagueMembershipsViewModel(dto, currentUserId); } /** * Create a new league */ async createLeague(input: CreateLeagueInputDTO): Promise { const dto = await this.apiClient.create(input); return new CreateLeagueViewModel(dto); } /** * Remove a member from league */ async removeMember(leagueId: string, performerDriverId: string, targetDriverId: string): Promise { const dto = await this.apiClient.removeMember(leagueId, performerDriverId, targetDriverId); return new RemoveMemberViewModel(dto); } }