// Reads bundled-skills/ and generates src/skills/embedded-skills.js
// so the installer works without filesystem access to bundled-skills/.
'use strict';

const fs = require('fs');
const path = require('path');

const repoRoot = path.resolve(__dirname, '..');
const bundledRoot = path.join(repoRoot, 'bundled-skills');
const outPath = path.join(repoRoot, 'src', 'skills', 'embedded-skills.js');

function walk(dir, prefix) {
  const entries = {};
  for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
    const rel = prefix ? prefix + '/' + entry.name : entry.name;
    if (entry.isDirectory()) {
      Object.assign(entries, walk(path.join(dir, entry.name), rel));
    } else {
      entries[rel] = fs.readFileSync(path.join(dir, entry.name), 'utf8');
    }
  }
  return entries;
}

if (!fs.existsSync(bundledRoot)) {
  throw new Error('bundled-skills/ not found — run npm run sync:bundled-skills first');
}

const entries = walk(bundledRoot, '');

const lines = [
  '// Auto-generated by scripts/embed-skills.js — do not edit manually.',
  '"use strict";',
  '',
  'const EMBEDDED_SKILLS = ' + JSON.stringify(entries) + ';',
  '',
  'module.exports = EMBEDDED_SKILLS;',
  ''
];

fs.writeFileSync(outPath, lines.join('\n'), 'utf8');
console.log(`Embedded ${Object.keys(entries).length} skill files into ${path.relative(repoRoot, outPath)}`);
