/*
  achievements.js — Save Pet
  Achievement definitions + a check() that derives progress from
  the save file (no separate fragile counters to get out of sync).
*/
window.SP = window.SP || {};

SP.Achievements = (function () {
  "use strict";

  function countLevelsWithStars(d, stars) {
    let c = 0;
    Object.keys(d.levels).forEach(function (k) { if (d.levels[k].stars >= stars) c++; });
    return c;
  }
  function countCompleted(d) {
    let c = 0;
    Object.keys(d.levels).forEach(function (k) { if (d.levels[k].completed) c++; });
    return c;
  }
  function countCompletedWithoutHint(d) {
    let c = 0;
    Object.keys(d.levels).forEach(function (k) { if (d.levels[k].completed && !d.levels[k].hintUsed) c++; });
    return c;
  }

  const DEFS = [
    { id: "first_steps", name: "First Steps", desc: "Complete Level 1", icon: "\uD83D\uDC3E", check: function (d) { return !!(d.levels["1"] && d.levels["1"].completed); } },
    { id: "bee_whisperer", name: "Bee Whisperer", desc: "Pop 100 bees", icon: "\uD83D\uDC1D", check: function (d) { return d.stats.totalBeesPopped >= 100; } },
    { id: "swarm_master", name: "Swarm Master", desc: "Pop 1,000 bees", icon: "\uD83C\uDF2A\uFE0F", check: function (d) { return d.stats.totalBeesPopped >= 1000; } },
    { id: "perfectionist", name: "Perfectionist", desc: "Earn 3 stars on 10 levels", icon: "\u2B50", check: function (d) { return countLevelsWithStars(d, 3) >= 10; } },
    { id: "untouchable", name: "Untouchable", desc: "Clear a level with zero hits", icon: "\uD83D\uDEE1\uFE0F", check: function (d) { return countLevelsWithStars(d, 3) >= 1; } },
    { id: "world_traveler", name: "World Traveler", desc: "Reach the final world", icon: "\uD83D\uDDFA\uFE0F", check: function (d) { return d.unlockedLevel > 90; } },
    { id: "century_club", name: "Century Club", desc: "Complete all 100 levels", icon: "\uD83C\uDFC6", check: function (d) { return countCompleted(d) >= 100; } },
    { id: "coin_collector", name: "Coin Collector", desc: "Earn 1,000 coins total", icon: "\uD83E\uDE99", check: function (d) { return d.stats.totalCoinsEarnedLifetime >= 1000; } },
    { id: "high_roller", name: "High Roller", desc: "Earn 10,000 coins total", icon: "\uD83D\uDCB0", check: function (d) { return d.stats.totalCoinsEarnedLifetime >= 10000; } },
    { id: "marathoner", name: "Marathoner", desc: "Survive 5 min in Endless Mode", icon: "\u23F1\uFE0F", check: function (d) { return d.endless.bestTimeSeconds >= 300; } },
    { id: "iron_will", name: "Iron Will", desc: "Survive 10 min in Endless Mode", icon: "\uD83D\uDCAA", check: function (d) { return d.endless.bestTimeSeconds >= 600; } },
    { id: "daily_devotee", name: "Daily Devotee", desc: "Complete 7 Daily Challenges", icon: "\uD83D\uDCC5", check: function (d) { return d.daily.totalCompleted >= 7; } },
    { id: "no_hints_needed", name: "No Hints Needed", desc: "Complete 20 levels without a hint", icon: "\uD83E\uDDE0", check: function (d) { return countCompletedWithoutHint(d) >= 20; } }
  ];

  function check() {
    const data = SP.Storage.data;
    const newly = [];
    DEFS.forEach(function (def) {
      const rec = data.achievements[def.id] || { unlocked: false };
      if (!rec.unlocked && def.check(data)) {
        data.achievements[def.id] = { unlocked: true, unlockedAt: Date.now() };
        newly.push(def);
      }
    });
    if (newly.length) {
      SP.Storage.save();
      newly.forEach(function (def) {
        if (SP.UI && SP.UI.showAchievementToast) SP.UI.showAchievementToast(def);
      });
    }
    return newly;
  }

  function getAll() {
    const data = SP.Storage.data;
    return DEFS.map(function (def) {
      const rec = data.achievements[def.id] || { unlocked: false };
      return { id: def.id, name: def.name, desc: def.desc, icon: def.icon, unlocked: !!rec.unlocked };
    });
  }

  return { DEFS: DEFS, check: check, getAll: getAll };
})();
