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

const SIGNAL_BATCH_BASE = '/signal-batch';
const CACHE_TTL_MS = 5 * 60_000; // 5분

let cachedList: ChnPrmShipInfo[] = [];
let cacheTime = 0;
let fetchPromise: Promise<ChnPrmShipInfo[]> | null = null;

async function fetchList(): Promise<ChnPrmShipInfo[]> {
  const now = Date.now();
  if (cachedList.length > 0 && now - cacheTime < CACHE_TTL_MS) {
    return cachedList;
  }

  if (fetchPromise) return fetchPromise;

  fetchPromise = (async () => {
    try {
      const res = await fetch(
        `${SIGNAL_BATCH_BASE}/api/v2/vessels/chnprmship/recent-positions?minutes=60`,
        { headers: { accept: 'application/json' } },
      );
      if (!res.ok) return cachedList;
      const json: unknown = await res.json();
      cachedList = Array.isArray(json) ? (json as ChnPrmShipInfo[]) : [];
      cacheTime = Date.now();
      return cachedList;
    } catch {
      return cachedList;
    } finally {
      fetchPromise = null;
    }
  })();

  return fetchPromise;
}

/** mmsi로 허가어선 정보 조회 — 목록을 캐시하고 lookup */
export async function lookupPermittedShip(mmsi: string): Promise<ChnPrmShipInfo | null> {
  const list = await fetchList();
  return list.find((s) => s.mmsi === mmsi) ?? null;
}

/** 허가어선 mmsi Set (빠른 조회용) */
export async function getPermittedMmsiSet(): Promise<Set<string>> {
  const list = await fetchList();
  return new Set(list.map((s) => s.mmsi));
}

/** 캐시 강제 갱신 */
export function invalidateCache(): void {
  cacheTime = 0;
}
