import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const MISSIONS_DIR = path.resolve(__dirname, "../assets/data/missions");

function verifyMissions() {
  console.log("Verifying missions in:", MISSIONS_DIR);
  const files = fs.readdirSync(MISSIONS_DIR).filter((f) => f.endsWith(".json"));

  let hasError = false;

  for (const file of files) {
    try {
      const filePath = path.join(MISSIONS_DIR, file);
      const content = fs.readFileSync(filePath, "utf8");
      const mission = JSON.parse(content);

      const missing = [];
      // Required root fields based on Mission.ts
      if (!mission.id) missing.push("id");
      if (!mission.type) missing.push("type");
      if (!mission.config) missing.push("config");
      if (!mission.biome) missing.push("biome");
      if (!mission.objectives) missing.push("objectives");
      if (!mission.rewards) missing.push("rewards");

      // Required children
      if (mission.config && !mission.config.title) missing.push("config.title");
      if (mission.config && !mission.config.description)
        missing.push("config.description");
      if (mission.config && mission.config.difficulty_tier === undefined)
        missing.push("config.difficulty_tier");

      if (mission.biome && !mission.biome.type) missing.push("biome.type");
      // generator_config is technically required in the interface `MissionBiome`
      // although older files might be missing it.
      if (mission.biome && !mission.biome.generator_config)
        missing.push("biome.generator_config");

      if (mission.objectives && !mission.objectives.primary)
        missing.push("objectives.primary");
      if (mission.rewards && !mission.rewards.guaranteed)
        missing.push("rewards.guaranteed");

      if (missing.length > 0) {
        console.log(
          `Mission ${file} is missing required fields: ${missing.join(", ")}`
        );
        hasError = true;
      }
    } catch (err) {
      console.error(`Error parsing ${file}:`, err.message);
      hasError = true;
    }
  }

  if (!hasError) {
    console.log("All missions pass structure verification.");
  }
}

verifyMissions();
