import fs from "node:fs";
import path from "node:path";

const root = process.cwd();
const cardsRoot = path.join(root, "assets", "cards");
const manifestPath = path.join(cardsRoot, "card-manifest.json");
const availablePath = path.join(cardsRoot, "available-assets.json");

const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
const assets = [];

for (const character of manifest.characters) {
  for (const art of character.art) {
    const absolute = path.join(root, art.path);
    if (!fs.existsSync(absolute)) continue;
    const stat = fs.statSync(absolute);
    assets.push({
      characterId: character.id,
      damageStage: art.damageStage,
      path: art.path.replaceAll("\\", "/"),
      bytes: stat.size,
      updatedAt: stat.mtime.toISOString(),
    });
  }
}

const payload = {
  schemaVersion: 1,
  generatedAt: new Date().toISOString(),
  expectedImageCount: manifest.expectedImageCount,
  completedImageCount: assets.length,
  completedCharacterCount: new Set(assets.map((asset) => asset.characterId)).size,
  assets,
};

fs.writeFileSync(availablePath, JSON.stringify(payload, null, 2), "utf8");
console.log(JSON.stringify(payload, null, 2));
