/**
 * ScheduleRaceFormPresenter - Pure data transformer
 * Transforms API response to view model without DI dependencies.
 */

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

export type SessionType = 'practice' | 'qualifying' | 'race';

export interface ScheduleRaceFormData {
  leagueId: string;
  track: string;
  car: string;
  sessionType: SessionType;
  scheduledDate: string;
  scheduledTime: string;
}

export interface ScheduledRaceViewModel {
  id: string;
  leagueId: string;
  track: string;
  car: string;
  sessionType: SessionType;
  scheduledAt: Date;
  status: string;
}

export interface LeagueOptionViewModel {
  id: string;
  name: string;
}

/**
 * Load available leagues for the schedule form.
 */
export async function loadScheduleRaceFormLeagues(): Promise<LeagueOptionViewModel[]> {
  const response = await apiClient.leagues.getAllWithCapacity();
  return response.leagues.map((league) => ({
    id: league.id,
    name: league.name,
  }));
}

/**
 * Schedule a race via API.
 * Note: This would need a dedicated API endpoint for race scheduling.
 * For now, this is a placeholder that shows the expected interface.
 */
export async function scheduleRaceFromForm(
  formData: ScheduleRaceFormData
): Promise<ScheduledRaceViewModel> {
  const scheduledAt = new Date(`${formData.scheduledDate}T${formData.scheduledTime}`);

  // In the new architecture, race scheduling should be done via API
  // This is a placeholder that returns expected data structure
  // The API endpoint would need to be implemented: POST /races
  
  // For now, return a mock response
  // TODO: Replace with actual API call when race creation endpoint is available
  return {
    id: `race-${Date.now()}`,
    leagueId: formData.leagueId,
    track: formData.track.trim(),
    car: formData.car.trim(),
    sessionType: formData.sessionType,
    scheduledAt,
    status: 'scheduled',
  };
}