/**
 * Schedule API functions.
 *
 * Pure functions -- each takes an `ApiClient` as a parameter (dependency
 * injection). No React hooks, no context, no side effects.
 *
 * @module
 */

import type { ApiClient } from "@/shared/api/client.js";
import type {
  IScheduleSearchRequest,
  IScheduleResponse,
  IScheduleDetailsResponse,
  IScheduleCalendarParams,
  IScheduleDaysResponse,
} from "./types.js";

// ---------------------------------------------------------------------------
// API functions
// ---------------------------------------------------------------------------

/**
 * Search schedule flights.
 * Maps to: `POST schedule/1` with body parameters.
 *
 * The API response is an array of flight objects (IFlight[]).
 */
export async function searchSchedule(
  client: ApiClient,
  params: IScheduleSearchRequest,
): Promise<IScheduleResponse> {
  return client.post<IScheduleResponse>(`flights/1/${client.locale}/schedule`, params);
}

/**
 * Get multi-flight schedule details.
 * Maps to: `GET schedule/details?flights[0]=...&dates[0]=...&departure=...&arrival=...`
 *
 * The Angular service uses indexed query params (flights[0], flights[1], etc.).
 * We build the query object to match that format.
 */
export async function getScheduleDetails(
  client: ApiClient,
  params: {
    flights: string[];
    dates: string[];
    departure: string;
    arrival: string;
  },
): Promise<IScheduleDetailsResponse> {
  const query: Record<string, string> = {
    departure: params.departure,
    arrival: params.arrival,
  };

  for (let i = 0; i < params.flights.length; i++) {
    const flight = params.flights[i];
    if (flight !== undefined) query[`flights[${i}]`] = flight;
  }

  for (let i = 0; i < params.dates.length; i++) {
    const date = params.dates[i];
    if (date !== undefined) query[`dates[${i}]`] = date;
  }

  return client.get<IScheduleDetailsResponse>(`flights/v1.1/${client.locale}/schedule/details`, query);
}

/**
 * Get available calendar days for a given route.
 * Maps to: `GET days/{date}/382/{param}/schedule/v1`
 *
 * The API returns `{ days: "2025-01-01,2025-01-02,..." }` -- a single
 * comma-separated string. This function splits it into `string[]`.
 */
export async function getScheduleCalendarDays(
  client: ApiClient,
  params: IScheduleCalendarParams,
): Promise<string[]> {
  const routeSegment = params.connections
    ? `connections/${params.departure}-${params.arrival}-1`
    : `route/${params.departure}-${params.arrival}`;

  const path = `flights/v1/${client.locale}/days/${params.date}/382/${routeSegment}/schedule/`;

  const response = await client.get<IScheduleDaysResponse>(path);
  return parseCalendarDays(response.days);
}

// ---------------------------------------------------------------------------
// Internal helpers
// ---------------------------------------------------------------------------

function parseCalendarDays(days: string): string[] {
  if (!days) return [];
  return days.split(",").map((d) => d.trim()).filter(Boolean);
}
