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

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

const ASSETS_DIR = path.resolve(__dirname, "../assets/data");
const OUTPUT_FILE = path.resolve(ASSETS_DIR, "manifest.json");

function getJsonFiles(dir) {
  if (!fs.existsSync(dir)) return [];
  return fs
    .readdirSync(dir)
    .filter((f) => f.endsWith(".json"))
    .map((f) => path.join(path.basename(dir), f).replace(/\\/g, "/"));
}

export function generateManifest() {
  console.log("Generating asset manifest...");

  const classes = getJsonFiles(path.join(ASSETS_DIR, "classes"));
  const units = getJsonFiles(path.join(ASSETS_DIR, "units"));

  const manifest = {
    classes,
    units,
  };

  fs.writeFileSync(OUTPUT_FILE, JSON.stringify(manifest, null, 2));
  console.log(`Manifest written to ${OUTPUT_FILE}`);
}

// Allow running directly
if (process.argv[1] === __filename) {
  generateManifest();
}
