import type { TeamsApiClient } from '../../api/teams/TeamsApiClient';
import type { TeamDetailsPresenter } from '../../presenters/TeamDetailsPresenter';
import type { TeamListPresenter } from '../../presenters/TeamListPresenter';
import type { TeamMembersPresenter } from '../../presenters/TeamMembersPresenter';
import type { TeamSummaryViewModel, TeamDetailsViewModel, TeamMemberViewModel } from '../../view-models';
import type { CreateTeamInputDto, CreateTeamOutputDto, UpdateTeamInputDto, UpdateTeamOutputDto, DriverTeamDto } from '../../dtos';

/**
 * Team Service
 *
 * Orchestrates team operations by coordinating API calls and presentation logic.
 * All dependencies are injected via constructor.
 */
export class TeamService {
  constructor(
    private readonly apiClient: TeamsApiClient,
    private readonly teamListPresenter: TeamListPresenter,
    private readonly teamDetailsPresenter: TeamDetailsPresenter,
    private readonly teamMembersPresenter: TeamMembersPresenter
  ) {}

  /**
   * Get all teams with presentation transformation
   */
  async getAllTeams(): Promise<TeamSummaryViewModel[]> {
    const dto = await this.apiClient.getAll();
    return this.teamListPresenter.present(dto);
  }

  /**
   * Get team details with presentation transformation
   */
  async getTeamDetails(teamId: string, currentUserId: string): Promise<TeamDetailsViewModel | null> {
    const dto = await this.apiClient.getDetails(teamId);
    if (!dto) {
      return null;
    }
    return this.teamDetailsPresenter.present(dto, currentUserId);
  }

  /**
   * Get team members with presentation transformation
   */
  async getTeamMembers(teamId: string, currentUserId: string, teamOwnerId: string): Promise<TeamMemberViewModel[]> {
    const dto = await this.apiClient.getMembers(teamId);
    return this.teamMembersPresenter.present(dto, currentUserId, teamOwnerId);
  }

  /**
   * Create a new team
   */
  async createTeam(input: CreateTeamInputDto): Promise<CreateTeamOutputDto> {
    return await this.apiClient.create(input);
  }

  /**
   * Update team
   */
  async updateTeam(teamId: string, input: UpdateTeamInputDto): Promise<UpdateTeamOutputDto> {
    return await this.apiClient.update(teamId, input);
  }

  /**
   * Get driver's team
   */
  async getDriverTeam(driverId: string): Promise<DriverTeamDto | null> {
    return await this.apiClient.getDriverTeam(driverId);
  }
}