import { TeamJoinRequestViewModel } from '@/lib/view-models/TeamJoinRequestViewModel';
import type { TeamsApiClient } from '../../api/teams/TeamsApiClient';
import type { TeamJoinRequestDTO } from '../../types/generated/TeamJoinRequestDTO';

// Wrapper for the team join requests collection returned by the teams API in this build
// Mirrors the current API response shape until a generated DTO is available.
type TeamJoinRequestsDto = {
  requests: TeamJoinRequestDTO[];
};

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

  /**
   * Get team join requests with view model transformation
   */
  async getJoinRequests(teamId: string, currentUserId: string, isOwner: boolean): Promise<TeamJoinRequestViewModel[]> {
    const dto = await this.apiClient.getJoinRequests(teamId) as TeamJoinRequestsDto | null;
    return (dto?.requests || []).map((r: TeamJoinRequestDTO) => new TeamJoinRequestViewModel(r, currentUserId, isOwner));
  }

  /**
   * Approve a team join request
   *
   * The teams API currently exposes read-only join requests in this build; approving
   * a request requires a future management endpoint, so this method fails explicitly.
   */
  async approveJoinRequest(): Promise<never> {
    throw new Error('Not implemented: API endpoint for approving join requests');
  }

  /**
   * Reject a team join request
   *
   * Rejection of join requests is also not available yet on the backend, so callers
   * must treat this as an unsupported operation rather than a silent no-op.
   */
  async rejectJoinRequest(): Promise<never> {
    throw new Error('Not implemented: API endpoint for rejecting join requests');
  }
}