/**
 * Download PixelLab map objects by id into frontend/assets paths.
 * Usage: node scripts/pixellab-download-map-objects.mjs rel/path.png <uuid> ...
 */
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const ROOT = path.join(__dirname, '..');

async function main() {
  const args = process.argv.slice(2);
  if (args.length < 2 || args.length % 2 !== 0) {
    console.error('Usage: rel/path.png <objectId> ...');
    process.exit(1);
  }
  for (let i = 0; i < args.length; i += 2) {
    const rel = args[i];
    const id = args[i + 1];
    const url = `https://api.pixellab.ai/mcp/map-objects/${id}/download`;
    const dest = path.join(ROOT, 'frontend/assets', rel);
    const res = await fetch(url);
    if (!res.ok) throw new Error(`${rel} GET ${res.status}`);
    fs.mkdirSync(path.dirname(dest), { recursive: true });
    fs.writeFileSync(dest, Buffer.from(await res.arrayBuffer()));
    console.log('OK', rel);
  }
}

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