import { TeamDetailsViewModel } from '@/lib/view-models/TeamDetailsViewModel';
import { TeamMemberViewModel } from '@/lib/view-models/TeamMemberViewModel';
import { TeamSummaryViewModel } from '@/lib/view-models/TeamSummaryViewModel';
import { CreateTeamViewModel } from '@/lib/view-models/CreateTeamViewModel';
import { UpdateTeamViewModel } from '@/lib/view-models/UpdateTeamViewModel';
import { DriverTeamViewModel } from '@/lib/view-models/DriverTeamViewModel';
import type { TeamsApiClient } from '../../api/teams/TeamsApiClient';
import type { GetAllTeamsOutputDTO } from '@/lib/types/generated/GetAllTeamsOutputDTO';
import type { TeamListItemDTO } from '@/lib/types/generated/TeamListItemDTO';
import type { GetTeamDetailsOutputDTO } from '@/lib/types/generated/GetTeamDetailsOutputDTO';
import type { GetTeamMembersOutputDTO } from '@/lib/types/generated/GetTeamMembersOutputDTO';
import type { TeamMemberDTO } from '@/lib/types/generated/TeamMemberDTO';
import type { CreateTeamInputDTO } from '@/lib/types/generated/CreateTeamInputDTO';
import type { CreateTeamOutputDTO } from '@/lib/types/generated/CreateTeamOutputDTO';
import type { UpdateTeamInputDTO } from '@/lib/types/generated/UpdateTeamInputDTO';
import type { UpdateTeamOutputDTO } from '@/lib/types/generated/UpdateTeamOutputDTO';
import type { GetDriverTeamOutputDTO } from '@/lib/types/generated/GetDriverTeamOutputDTO';
import type { GetTeamMembershipOutputDTO } from '@/lib/types/generated/GetTeamMembershipOutputDTO';

/**
 * Team Service
 *
 * Orchestrates team operations by coordinating API calls and view model creation.
 * All dependencies are injected via constructor.
 */
export class TeamService {
  constructor(
    private readonly apiClient: TeamsApiClient
  ) {}

  /**
   * Get all teams with view model transformation
   */
  async getAllTeams(): Promise<TeamSummaryViewModel[]> {
    const dto: GetAllTeamsOutputDTO | null = await this.apiClient.getAll();
    return (dto?.teams || []).map((team: TeamListItemDTO) => new TeamSummaryViewModel(team));
  }

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

  /**
   * Get team members with view model transformation
   */
  async getTeamMembers(teamId: string, currentUserId: string, teamOwnerId: string): Promise<TeamMemberViewModel[]> {
    const dto: GetTeamMembersOutputDTO = await this.apiClient.getMembers(teamId);
    return dto.members.map((member: TeamMemberDTO) => new TeamMemberViewModel(member, currentUserId, teamOwnerId));
  }

  /**
   * Create a new team with view model transformation
   */
  async createTeam(input: CreateTeamInputDTO): Promise<CreateTeamViewModel> {
    const dto: CreateTeamOutputDTO = await this.apiClient.create(input);
    return new CreateTeamViewModel(dto);
  }

  /**
   * Update team with view model transformation
   */
  async updateTeam(teamId: string, input: UpdateTeamInputDTO): Promise<UpdateTeamViewModel> {
    const dto: UpdateTeamOutputDTO = await this.apiClient.update(teamId, input);
    return new UpdateTeamViewModel(dto);
  }

  /**
   * Get driver's team with view model transformation
   */
  async getDriverTeam(driverId: string): Promise<DriverTeamViewModel | null> {
    const dto: GetDriverTeamOutputDTO | null = await this.apiClient.getDriverTeam(driverId);
    return dto ? new DriverTeamViewModel(dto) : null;
  }

  /**
   * Get team membership for a driver
   */
  async getMembership(teamId: string, driverId: string): Promise<GetTeamMembershipOutputDTO | null> {
    return this.apiClient.getMembership(teamId, driverId);
  }

   /**
    * Remove a driver from the team
    *
    * The backend does not yet expose a dedicated endpoint for removing team memberships,
    * so this method fails explicitly to avoid silently ignoring removal requests.
    */
  async removeMembership(teamId: string, driverId: string): Promise<void> {
    void teamId;
    void driverId;
    throw new Error('Team membership removal is not supported in this build');
  }

   /**
    * Update team membership role
    *
    * Role updates for team memberships are not supported by the current API surface;
    * callers must treat this as an unavailable operation.
    */
  async updateMembership(teamId: string, driverId: string, role: string): Promise<void> {
    void teamId;
    void driverId;
    void role;
    throw new Error('Team membership role updates are not supported in this build');
  }
}
