// ═══ Korean Power Infrastructure from OpenStreetMap (Overpass API) ═══

export interface PowerFacility {
  id: string;
  type: 'plant' | 'substation';
  name: string;
  lat: number;
  lng: number;
  source?: string;    // solar, nuclear, gas, coal, wind, hydro, oil
  output?: string;    // e.g. "1000 MW"
  operator?: string;
  voltage?: string;   // for substations
}

let cachedData: PowerFacility[] | null = null;
let lastFetch = 0;
const CACHE_MS = 600_000; // 10 min cache

export async function fetchKoreaInfra(): Promise<PowerFacility[]> {
  if (cachedData && Date.now() - lastFetch < CACHE_MS) return cachedData;

  // 정적 데이터 사용 (Overpass API는 프로덕션 nginx에서 미지원 + fallback 데이터로 충분)
  cachedData = getFallbackInfra();
  lastFetch = Date.now();
  return cachedData;
}

// Fallback: major Korean power plants (in case API fails)
function getFallbackInfra(): PowerFacility[] {
  return [
    // Nuclear
    { id: 'p-kori', type: 'plant', name: '고리 원전', lat: 35.3197, lng: 129.2783, source: 'nuclear', output: '6040 MW', operator: '한국수력원자력' },
    { id: 'p-shin-kori', type: 'plant', name: '신고리 원전', lat: 35.3220, lng: 129.2900, source: 'nuclear', output: '5600 MW', operator: '한국수력원자력' },
    { id: 'p-hanul', type: 'plant', name: '한울 원전', lat: 37.0928, lng: 129.3844, source: 'nuclear', output: '5900 MW', operator: '한국수력원자력' },
    { id: 'p-hanbit', type: 'plant', name: '한빛 원전', lat: 35.4133, lng: 126.4228, source: 'nuclear', output: '5875 MW', operator: '한국수력원자력' },
    { id: 'p-wolsong', type: 'plant', name: '월성 원전', lat: 35.7128, lng: 129.4753, source: 'nuclear', output: '2779 MW', operator: '한국수력원자력' },
    { id: 'p-shin-wolsong', type: 'plant', name: '신월성 원전', lat: 35.7100, lng: 129.4800, source: 'nuclear', output: '2000 MW', operator: '한국수력원자력' },
    // Coal/Gas
    { id: 'p-dangjin', type: 'plant', name: '당진 화력', lat: 36.9703, lng: 126.6067, source: 'coal', output: '6040 MW', operator: '한국동서발전' },
    { id: 'p-taean', type: 'plant', name: '태안 화력', lat: 36.7833, lng: 126.2500, source: 'coal', output: '6100 MW', operator: '한국서부발전' },
    { id: 'p-boryeong', type: 'plant', name: '보령 화력', lat: 36.3500, lng: 126.4833, source: 'coal', output: '4000 MW', operator: '한국중부발전' },
    { id: 'p-samcheok', type: 'plant', name: '삼척 화력', lat: 37.3667, lng: 129.1500, source: 'coal', output: '2100 MW', operator: '한국남부발전' },
    { id: 'p-incheon', type: 'plant', name: '인천 LNG', lat: 37.4483, lng: 126.5917, source: 'gas', output: '3478 MW', operator: '한국중부발전' },
    { id: 'p-pyeongtaek', type: 'plant', name: '평택 LNG', lat: 36.9667, lng: 126.9333, source: 'gas', output: '1770 MW', operator: '한국중부발전' },
    { id: 'p-yeongheung', type: 'plant', name: '영흥 화력', lat: 37.2167, lng: 126.4333, source: 'coal', output: '5080 MW', operator: '한국남동발전' },
    // Hydro
    { id: 'p-chungju', type: 'plant', name: '충주 수력', lat: 36.9833, lng: 127.9833, source: 'hydro', output: '412 MW', operator: '한국수력원자력' },
    { id: 'p-hapcheon', type: 'plant', name: '합천 수력', lat: 35.5667, lng: 128.1500, source: 'hydro', output: '360 MW', operator: '한국수력원자력' },
    // Wind
    { id: 'p-yeongdeok', type: 'plant', name: '영덕 풍력', lat: 36.4150, lng: 129.4300, source: 'wind', output: '39.6 MW', operator: '한국남동발전' },
    { id: 'p-taebaek', type: 'plant', name: '태백 풍력', lat: 37.1500, lng: 128.9800, source: 'wind', output: '40 MW', operator: '한국동서발전' },
    { id: 'p-gasiri', type: 'plant', name: '가시리 풍력 (제주)', lat: 33.3600, lng: 126.6800, source: 'wind', output: '15 MW', operator: '제주에너지공사' },
    { id: 'p-tamra', type: 'plant', name: '탐라 해상풍력 (제주)', lat: 33.2800, lng: 126.1700, source: 'wind', output: '30 MW', operator: '탐라해상풍력' },
    { id: 'p-seonam', type: 'plant', name: '서남해 해상풍력', lat: 35.0700, lng: 126.0200, source: 'wind', output: '60 MW', operator: '한국해상풍력' },
    { id: 'p-yeongyang', type: 'plant', name: '영양 풍력', lat: 36.7000, lng: 129.1200, source: 'wind', output: '61.5 MW', operator: '한국남부발전' },
    { id: 'p-jeongseon', type: 'plant', name: '정선 풍력', lat: 37.3300, lng: 128.7200, source: 'wind', output: '30 MW', operator: '강원풍력' },
    { id: 'p-daegwallyeong', type: 'plant', name: '대관령 풍력', lat: 37.7000, lng: 128.7500, source: 'wind', output: '98 MW', operator: '대관령풍력' },
    { id: 'p-sinan', type: 'plant', name: '신안 해상풍력', lat: 34.8500, lng: 125.8800, source: 'wind', output: '8.2 GW', operator: '신안해상풍력' },
    { id: 'p-ulsan-float', type: 'plant', name: '울산 부유식 해상풍력', lat: 35.4000, lng: 129.6500, source: 'wind', output: '1.5 GW', operator: '울산부유식풍력' },
    // Major substations
    { id: 's-singapyeong', type: 'substation', name: '신갑평 변전소', lat: 37.1667, lng: 127.3000, voltage: '765000', operator: 'KEPCO' },
    { id: 's-sinseosan', type: 'substation', name: '신서산 변전소', lat: 36.7500, lng: 126.5000, voltage: '765000', operator: 'KEPCO' },
    { id: 's-sinchungju', type: 'substation', name: '신충주 변전소', lat: 36.9833, lng: 127.9500, voltage: '765000', operator: 'KEPCO' },
    { id: 's-sinyongin', type: 'substation', name: '신용인 변전소', lat: 37.2333, lng: 127.2000, voltage: '765000', operator: 'KEPCO' },
    { id: 's-sinbukgyeongnam', type: 'substation', name: '신북경남 변전소', lat: 35.4500, lng: 128.7500, voltage: '765000', operator: 'KEPCO' },
  ];
}
