/* Scrape Foundry VTT pf2e class packs (OGL/ORC + Paizo Community Use) →
 * normalized JSON in public/data/pf2e/classes.json.
 * Run: bunx tsx scripts/fetch_foundry_pf2e.ts */
import { writeFileSync } from 'node:fs';
import { normalizeFoundryClass, type FoundryClass } from '../src/lib/ruleset/normalize';

const LIST = 'https://api.github.com/repos/foundryvtt/pf2e/contents/packs/classes?ref=master';

async function main() {
  const res = await fetch(LIST, { headers: { 'User-Agent': 'ttrpg-manager-scraper', Accept: 'application/vnd.github+json' } });
  if (!res.ok) throw new Error(`list: ${res.status}`);
  const files = (await res.json()) as { name: string; download_url: string }[];
  const classFiles = files.filter((f) => f.name.endsWith('.json'));

  const out = [];
  for (const f of classFiles) {
    const raw = (await (await fetch(f.download_url)).json()) as FoundryClass;
    out.push(normalizeFoundryClass(f.name.replace(/\.json$/, ''), raw));
  }
  out.sort((a, b) => a.name.localeCompare(b.name));
  writeFileSync('public/data/pf2e/classes.json', JSON.stringify(out));
  console.log(`pf2e: classes ${out.length} (${out.map((c) => c.name).join(', ')})`);
}

main().catch((e) => { console.error(e); process.exit(1); });
