// ============================================================
// game_engine.jsx  —  공통 게임 엔진 유틸리티
// 모든 게임 컴포넌트가 사용하는 공유 레이어
// ============================================================

const GameEngine = (() => {
  const TOKEN_KEY = 'game_token';

  // ── 인증 헤더 ──────────────────────────────────────────
  function authHeader() {
    const t = localStorage.getItem(TOKEN_KEY);
    return t ? { 'Authorization': 'Bearer ' + t } : {};
  }

  // ── 유저 + 게임 상태 조회 ───────────────────────────────
  async function getMe() {
    const res = await fetch('/api/game/me', { headers: authHeader() });
    return res.json();
  }

  // ── 세션 저장 + EXP 적립 ──────────────────────────────
  async function saveSession({ gameId, moduleType, score, durationSec, metadata }) {
    const res = await fetch('/api/game/session', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json', ...authHeader() },
      body: JSON.stringify({
        game_id: gameId,
        module_type: moduleType,
        score,
        duration_sec: durationSec,
        metadata,
      }),
    });
    return res.json();
  }

  // ── SCT 문장 AI 변환 ────────────────────────────────────
  async function transformSentence(text) {
    const res = await fetch('/api/game/ai-transform', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json', ...authHeader() },
      body: JSON.stringify({ text }),
    });
    return res.json();
  }

  // ── 정원 시각 상태 업데이트 ─────────────────────────────
  async function updateVisual(phq9Score) {
    const res = await fetch('/api/game/visual', {
      method: 'PATCH',
      headers: { 'Content-Type': 'application/json', ...authHeader() },
      body: JSON.stringify({ phq9_score: phq9Score }),
    });
    return res.json();
  }

  // ── 레벨 정보 계산 ──────────────────────────────────────
  const LEVELS = [
    { level:1, name:'씨앗',    emoji:'🌱', minExp:0,    maxExp:100  },
    { level:2, name:'새싹',    emoji:'🌿', minExp:100,  maxExp:250  },
    { level:3, name:'꽃봉오리', emoji:'🌸', minExp:250,  maxExp:500  },
    { level:4, name:'꽃피움',  emoji:'🌺', minExp:500,  maxExp:900  },
    { level:5, name:'만개',    emoji:'🌳', minExp:900,  maxExp:1500 },
    { level:6, name:'정원사',  emoji:'🏡', minExp:1500, maxExp:9999 },
  ];

  function getLevelInfo(exp) {
    const info = [...LEVELS].reverse().find(l => exp >= l.minExp) || LEVELS[0];
    const nextLevel = LEVELS[info.level] || null; // level is 1-indexed, array 0-indexed
    const progress = nextLevel
      ? Math.round(((exp - info.minExp) / (info.maxExp - info.minExp)) * 100)
      : 100;
    return { ...info, nextLevel, progress, currentExp: exp };
  }

  // ── 정원 상태 → 배경 스타일 매핑 ──────────────────────
  const GARDEN_THEMES = {
    foggy: {
      sky:    ['#9BA8B0', '#C5CFD6', '#D8DFE4'],
      ground: '#7A8A7A',
      label:  '안개가 자욱한 정원',
      desc:   '지금은 많이 힘드시죠. 함께 조금씩 걷어내요.',
    },
    clearing: {
      sky:    ['#7FA8C8', '#A8C8E0', '#C8DFF0'],
      ground: '#5E8A4E',
      label:  '맑아지는 정원',
      desc:   '안개가 걷히고 있어요. 잘 하고 계세요.',
    },
    blooming: {
      sky:    ['#4A8EC2', '#7BB8D8', '#A8D4E8'],
      ground: '#4A7A3E',
      label:  '꽃이 피는 정원',
      desc:   '정원이 활짝 피어있어요. 오늘도 수고했어요.',
    },
  };

  function getGardenTheme(visualStatus) {
    return GARDEN_THEMES[visualStatus] || GARDEN_THEMES.clearing;
  }

  // ── 업적 정보 ───────────────────────────────────────────
  const ACHIEVEMENTS = {
    first_play: { name: '첫 발걸음',   emoji: '🚶', desc: '처음으로 게임을 플레이했어요' },
    streak_3:   { name: '3일 연속',    emoji: '🔥', desc: '3일 연속 방문했어요' },
    streak_7:   { name: '일주일의 기적', emoji: '⭐', desc: '7일 연속 방문했어요' },
    level_3:    { name: '꽃봉오리',    emoji: '🌸', desc: '레벨 3에 도달했어요' },
    level_5:    { name: '만개',        emoji: '🌳', desc: '레벨 5에 도달했어요' },
    exp_500:    { name: '성실한 정원사', emoji: '🏅', desc: '경험치 500 달성' },
  };

  function getAchievementInfo(id) {
    return ACHIEVEMENTS[id] || { name: id, emoji: '🏆', desc: '' };
  }

  // ── 시간 포맷 ───────────────────────────────────────────
  function formatDuration(sec) {
    if (sec < 60) return `${sec}초`;
    return `${Math.floor(sec / 60)}분 ${sec % 60}초`;
  }

  function formatRelativeTime(iso) {
    const diff = Date.now() - new Date(iso).getTime();
    const min = Math.floor(diff / 60000);
    if (min < 1) return '방금 전';
    if (min < 60) return `${min}분 전`;
    const hr = Math.floor(min / 60);
    if (hr < 24) return `${hr}시간 전`;
    return `${Math.floor(hr / 24)}일 전`;
  }

  // ── 크레딧 잔액 실시간 조회 ───────────────────────────
  async function getCredits() {
    const res = await fetch('/api/game/credits', { headers: authHeader() });
    return res.json();
  }

  // ── 게임 크레딧 차감 ────────────────────────────────────
  // returns: { success, data: { balance, spent } } | { success:false, error, errorCode, balance }
  async function spendCredit(gameId, amount) {
    if (!amount || amount <= 0) return { success: true, data: { balance: null, spent: 0 } };
    const res = await fetch('/api/game/spend-credit', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json', ...authHeader() },
      body: JSON.stringify({ game_id: gameId, amount }),
    });
    return res.json();
  }

  return {
    getMe, saveSession, transformSentence, updateVisual,
    getCredits, spendCredit,
    getLevelInfo, getGardenTheme, getAchievementInfo,
    formatDuration, formatRelativeTime,
    LEVELS,
  };
})();

// 마음풀 URL (maumgame은 마음풀에서 JWT SSO로 진입)
const PHYWEB_URL = (() => {
  const host = window.location.hostname;
  if (host === 'localhost' || host === '127.0.0.1') return 'http://localhost:3001';
  return 'https://maumful.com';  // 마음풀 메인 홈페이지
})();
// 별칭 (기존 코드 호환)
const MAUMFUL_URL = PHYWEB_URL;
