/**
 * TeamAdminPresenter - Pure data transformer
 * Transforms API responses to view models without DI dependencies.
 * All data fetching is done via apiClient.
 */

import { apiClient } from '@/lib/apiClient';
import type { DriverDTO } from '@/lib/apiClient';

// ============================================================================
// View Model Types
// ============================================================================

export interface TeamAdminJoinRequestViewModel {
  id: string;
  teamId: string;
  driverId: string;
  requestedAt: Date;
  message?: string | undefined;
  driver?: DriverDTO | undefined;
}

export interface TeamAdminTeamSummaryViewModel {
  id: string;
  name: string;
  tag: string;
  description: string;
  ownerId: string;
}

export interface TeamAdminViewModel {
  team: TeamAdminTeamSummaryViewModel;
  requests: TeamAdminJoinRequestViewModel[];
}

// ============================================================================
// Data Fetching Functions (using apiClient)
// ============================================================================

/**
 * Load team admin view model via API.
 */
export async function loadTeamAdminViewModel(
  team: TeamAdminTeamSummaryViewModel
): Promise<TeamAdminViewModel> {
  const requests = await loadTeamJoinRequests(team.id);
  return {
    team: {
      id: team.id,
      name: team.name,
      tag: team.tag,
      description: team.description,
      ownerId: team.ownerId,
    },
    requests,
  };
}

/**
 * Load join requests for a team via API.
 */
export async function loadTeamJoinRequests(
  teamId: string
): Promise<TeamAdminJoinRequestViewModel[]> {
  const response = await apiClient.teams.getJoinRequests(teamId);
  
  return response.requests.map((req) => {
    const viewModel: TeamAdminJoinRequestViewModel = {
      id: req.id,
      teamId: req.teamId,
      driverId: req.driverId,
      requestedAt: new Date(req.requestedAt),
    };

    if (req.message) {
      viewModel.message = req.message;
    }

    return viewModel;
  });
}

/**
 * Approve a team join request and return updated request view models.
 */
export async function approveTeamJoinRequestAndReload(
  requestId: string,
  teamId: string
): Promise<TeamAdminJoinRequestViewModel[]> {
  await apiClient.teams.approveJoinRequest(teamId, requestId);
  return loadTeamJoinRequests(teamId);
}

/**
 * Reject a team join request and return updated request view models.
 */
export async function rejectTeamJoinRequestAndReload(
  requestId: string,
  teamId: string
): Promise<TeamAdminJoinRequestViewModel[]> {
  await apiClient.teams.rejectJoinRequest(teamId, requestId);
  return loadTeamJoinRequests(teamId);
}

/**
 * Update team basic details via API.
 */
export async function updateTeamDetails(params: {
  teamId: string;
  name: string;
  tag: string;
  description: string;
  updatedByDriverId: string;
}): Promise<void> {
  await apiClient.teams.update(params.teamId, {
    name: params.name,
    description: params.description,
  });
}