// ============================================
// ARROWS GAME - Storage / Persistence Layer
// ============================================

const STORAGE_KEYS = {
  currentLevel:    'ag_currentLevel',
  unlockedLevels:  'ag_unlockedLevels',
  completedLevels: 'ag_completedLevels',
  coins:           'ag_coins',
  lives:           'ag_lives',
  muted:           'ag_muted',
  theme:           'ag_theme',
  totalPlays:      'ag_totalPlays',
  highScores:      'ag_highScores',
  installDismissed: 'ag_installDismissed',
};

const Storage = {
  // ── Current Level ──────────────────────────
  getCurrentLevel() {
    return parseInt(localStorage.getItem(STORAGE_KEYS.currentLevel) || '1');
  },
  setCurrentLevel(n) {
    localStorage.setItem(STORAGE_KEYS.currentLevel, n);
  },

  // ── Unlocked Levels ────────────────────────
  getUnlockedLevels() {
    try {
      return JSON.parse(localStorage.getItem(STORAGE_KEYS.unlockedLevels) || '[1]');
    } catch { return [1]; }
  },
  unlockLevel(n) {
    const arr = this.getUnlockedLevels();
    if (!arr.includes(n)) { arr.push(n); localStorage.setItem(STORAGE_KEYS.unlockedLevels, JSON.stringify(arr)); }
  },
  isUnlocked(n) {
    return this.getUnlockedLevels().includes(n);
  },

  // ── Completed Levels ───────────────────────
  getCompletedLevels() {
    try {
      return JSON.parse(localStorage.getItem(STORAGE_KEYS.completedLevels) || '[]');
    } catch { return []; }
  },
  completeLevel(n) {
    const arr = this.getCompletedLevels();
    if (!arr.includes(n)) { arr.push(n); localStorage.setItem(STORAGE_KEYS.completedLevels, JSON.stringify(arr)); }
    this.unlockLevel(n + 1);
  },
  isCompleted(n) {
    return this.getCompletedLevels().includes(n);
  },

  // ── Coins ──────────────────────────────────
  getCoins() {
    return parseInt(localStorage.getItem(STORAGE_KEYS.coins) || String(GAME_CONFIG.startingCoins));
  },
  setCoins(n) {
    localStorage.setItem(STORAGE_KEYS.coins, Math.max(0, n));
  },
  addCoins(n) {
    this.setCoins(this.getCoins() + n);
  },
  spendCoins(n) {
    const current = this.getCoins();
    if (current < n) return false;
    this.setCoins(current - n);
    return true;
  },

  // ── Lives ──────────────────────────────────
  getLives() {
    const stored = localStorage.getItem(STORAGE_KEYS.lives);
    return stored !== null ? parseInt(stored) : GAME_CONFIG.defaultLives;
  },
  setLives(n) {
    localStorage.setItem(STORAGE_KEYS.lives, Math.max(0, Math.min(n, GAME_CONFIG.maxLives)));
  },
  loseLife() {
    this.setLives(this.getLives() - 1);
  },
  restoreLives() {
    this.setLives(GAME_CONFIG.defaultLives);
  },

  // ── Mute ───────────────────────────────────
  getMuted() {
    return localStorage.getItem(STORAGE_KEYS.muted) === 'true';
  },
  setMuted(v) {
    localStorage.setItem(STORAGE_KEYS.muted, v);
  },

  // ── Theme (game screen dark/light) ─────────
  getTheme() {
    return localStorage.getItem(STORAGE_KEYS.theme) || 'light';
  },
  setTheme(v) {
    localStorage.setItem(STORAGE_KEYS.theme, v);
  },

  // ── Install prompt dismissal ────────────────
  getInstallDismissed() {
    return localStorage.getItem(STORAGE_KEYS.installDismissed) === 'true';
  },
  setInstallDismissed(v) {
    localStorage.setItem(STORAGE_KEYS.installDismissed, v);
  },

  // ── Reset ──────────────────────────────────
  resetAll() {
    Object.values(STORAGE_KEYS).forEach(k => localStorage.removeItem(k));
  },

  // ── Stats ──────────────────────────────────
  incrementPlays() {
    const n = parseInt(localStorage.getItem(STORAGE_KEYS.totalPlays) || '0') + 1;
    localStorage.setItem(STORAGE_KEYS.totalPlays, n);
    return n;
  },
  getTotalPlays() {
    return parseInt(localStorage.getItem(STORAGE_KEYS.totalPlays) || '0');
  },
};
