const API_BASE = '/api/kcg/sensor';

export interface SeismicDto {
  usgsId: string;
  magnitude: number;
  depth: number | null;
  lat: number;
  lng: number;
  place: string;
  timestamp: number; // epoch ms
}

export interface PressureDto {
  station: string;
  lat: number;
  lng: number;
  pressureHpa: number;
  timestamp: number; // epoch ms
}

interface SensorResponse<T> {
  count: number;
  data: T[];
}

/**
 * 지진 이벤트 조회
 * @param min 조회 범위 (분, 기본 2880=48h)
 */
export async function fetchSeismic(min?: number): Promise<SeismicDto[]> {
  const params = min != null ? `?min=${min}` : '';
  const res = await fetch(`${API_BASE}/seismic${params}`);
  if (!res.ok) throw new Error(`seismic API ${res.status}`);
  const body: SensorResponse<SeismicDto> = await res.json();
  return body.data;
}

/**
 * 기압 데이터 조회
 * @param min 조회 범위 (분, 기본 2880=48h)
 */
export async function fetchPressure(min?: number): Promise<PressureDto[]> {
  const params = min != null ? `?min=${min}` : '';
  const res = await fetch(`${API_BASE}/pressure${params}`);
  if (!res.ok) throw new Error(`pressure API ${res.status}`);
  const body: SensorResponse<PressureDto> = await res.json();
  return body.data;
}
