// GachaPlan — 데이터 로더
// 모든 데이터는 /api/* (server.js → kv-data/*.json)에서 가져옵니다.
// server.js가 실행 중이 아니면 빈 화면이 표시됩니다.

// ── UI 상수 (변경 빈도 낮음 — 코드에 고정) ────────────────────────────
const RELEASE_GAME = { id: "new", short: "NEW", name: "신작", color: "#6af7a0", noImg: true };

const CATEGORIES = {
  pickup:    { id: "pickup",    label: "픽업",     color: "#f78c3a", tag: "픽업" },
  ingame:    { id: "ingame",    label: "이벤트",   color: "#f7c040", tag: "이벤트" },
  update:    { id: "update",    label: "업데이트", color: "#60b8f0", tag: "업데이트" },
  offline:   { id: "offline",   label: "오프라인", color: "#f7886a", tag: "행사" },
  broadcast: { id: "broadcast", label: "공식방송", color: "#6ab4f7", tag: "방송" },
  release:   { id: "release",   label: "신작",     color: "#6af7a0", tag: "신작" },
  cbt:       { id: "cbt",       label: "CBT",      color: "#c080f7", tag: "CBT" },
};

// 게임 목록: 신규 게임 추가 시에만 수정 (뉴스·필터가 서버 없이도 동작해야 함)
const GAMES = [
  { id: "ba",    short: "BA",  name: "블루 아카이브",   dev: "넥슨게임즈",     color: "#2aa8e0", launch: "2021-11-08" },
  { id: "gi",    short: "GI",  name: "원신",            dev: "HoYoverse",     color: "#e89820", launch: "2020-09-28" },
  { id: "hsr",   short: "HSR", name: "스타레일",        dev: "HoYoverse",     color: "#8860f2", launch: "2023-04-26" },
  { id: "nikke", short: "NK",  name: "니케",            dev: "시프트업",       color: "#ff4080", launch: "2022-11-04" },
  { id: "ak",    short: "AK",  name: "명일방주",        dev: "Hypergryph",    color: "#c8b060", launch: "2020-01-16" },
  { id: "zzz",   short: "ZZ",  name: "젠레스 존 제로",  dev: "HoYoverse",     color: "#e83820", launch: "2024-07-04" },
  { id: "ww",    short: "WW",  name: "명조",            dev: "Kuro Games",    color: "#28c8b0", launch: "2024-05-22" },
  { id: "lc",    short: "LC",  name: "림버스 컴퍼니",   dev: "Project Moon",  color: "#c86828", launch: "2023-03-08" },
  { id: "ef",    short: "EF",  name: "엔드필드",        dev: "Hypergryph",    color: "#30b868", launch: "2025-05-07" },
  { id: "gfl2",  short: "G2",  name: "소녀전선 2",      dev: "MICA Team",     color: "#8aaa30", launch: "2025-04-24" },
  { id: "tr",    short: "TR",  name: "트릭컬 리바이브", dev: "Delight Games", color: "#e060f8", launch: "2024-04-02" },
  { id: "mg",    short: "MG",  name: "몬길: STAR DIVE", dev: "넷마블",         color: "#5068e8", launch: "2026-04-15" },
];

// ── 기본값 (API 로드 전) ────────────────────────────────────────────────
window.GACHA_GAMES      = GAMES;
window.GACHA_EVENTS     = [];
window.GACHA_CATEGORIES = CATEGORIES;
window.RELEASE_GAME     = RELEASE_GAME;
window.FAN_EVENTS       = [];
window.FAN_BOOTHS       = [];
window.SITE_NOTICE      = null;
window.CHANGELOG        = [];
window.SITE_CONFIG      = null;

// ── API에서 데이터 로드 ────────────────────────────────────────────────
async function initFromAPI() {
  if (!window.KV) {
    console.warn('[GachaPlan] window.KV 없음 — server.js를 실행해주세요 (node server.js)');
    return;
  }
  try {
    const [games, events, notices, booths, config] = await Promise.allSettled([
      KV.get('games'),
      KV.get('events'),
      KV.get('notices'),
      KV.get('booths'),
      KV.get('config'),
    ]);
    if (games.status   === 'fulfilled') window.GACHA_GAMES  = games.value;
    if (events.status  === 'fulfilled') window.GACHA_EVENTS = events.value;
    if (notices.status === 'fulfilled') {
      window.SITE_NOTICE = notices.value.banner;
      window.CHANGELOG   = notices.value.changelog;
    }
    if (booths.status  === 'fulfilled') {
      window.FAN_EVENTS = booths.value.fan_events;
      window.FAN_BOOTHS = booths.value.fan_booths;
    }
    if (config.status  === 'fulfilled') window.SITE_CONFIG  = config.value;
    window.dispatchEvent(new Event('gachaplan:data-loaded'));
    console.log('[GachaPlan] KV 데이터 로드 완료 ✓');
  } catch (e) {
    console.error('[GachaPlan] API 로드 실패:', e.message);
  }
}
initFromAPI();
