import type { Aircraft } from '../types';

interface AircraftApiResponse {
  region: string;
  count: number;
  lastUpdated: number;
  aircraft: Aircraft[];
}

export async function fetchAircraftFromBackend(region: 'iran' | 'korea'): Promise<Aircraft[]> {
  try {
    const res = await fetch(`/api/kcg/aircraft?region=${region}`, {
      credentials: 'include',
    });
    if (!res.ok) return [];
    const data: AircraftApiResponse = await res.json();
    return data.aircraft;
  } catch {
    return [];
  }
}

/** 시점 범위 조회 (리플레이용) */
export async function fetchAircraftByRange(region: string, from: string, to: string): Promise<Aircraft[]> {
  try {
    const res = await fetch(`/api/kcg/aircraft?region=${region}&from=${from}&to=${to}`, {
      credentials: 'include',
    });
    if (!res.ok) return [];
    const data = await res.json();
    return data.items ?? data.aircraft ?? [];
  } catch {
    return [];
  }
}
