module.exports = async function skill(args, flags) {
  const path = require("path");
  const sub = args[0];
  const name = args[1];
  const rootDir = process.cwd();
  const {
    disableSkill,
    enableSkill,
    listDisabledSkills,
    OVERLAY_PATH
  } = require("../lib/disabled-skills");

  if (!sub || sub === "help" || flags.help) {
    console.log(`
capint skill — disable / enable / pin without deleting files

  capint skill list [--json]
  capint skill disable <name>
  capint skill enable <name>
  capint skill pin <name>     promote to registry project_added tier

Overlay: ${OVERLAY_PATH}
`);
    return;
  }

  if (sub === "list") {
    const result = listDisabledSkills(rootDir);
    const { loadRegistry } = require("../lib/registry");
    const reg = loadRegistry(rootDir);
    const pinned = reg.registry?.project_added || [];
    if (flags.json) {
      console.log(JSON.stringify({ ...result, project_added: pinned }, null, 2));
      return;
    }
    console.log("\n## Disabled skills\n");
    if (result.parseError) {
      console.log(`Warning: invalid ${OVERLAY_PATH}`);
    }
    if (!result.count) {
      console.log("None");
    } else {
      for (const n of result.disabled) console.log(`  - ${n}`);
    }
    console.log("\n## Pinned (project_added)\n");
    if (!pinned.length) console.log("None");
    else for (const n of pinned) console.log(`  - ${n}`);
    console.log("");
    return;
  }

  if (sub === "disable") {
    if (!name) {
      console.error("Usage: capint skill disable <name>");
      process.exit(1);
    }
    const result = disableSkill(rootDir, name);
    if (result.error) {
      console.error(result.error);
      process.exit(1);
    }
    if (flags.json) {
      console.log(JSON.stringify(result, null, 2));
      return;
    }
    console.log(`Disabled: ${result.name}`);
    return;
  }

  if (sub === "enable") {
    if (!name) {
      console.error("Usage: capint skill enable <name>");
      process.exit(1);
    }
    const result = enableSkill(rootDir, name);
    if (result.error) {
      console.error(result.error);
      process.exit(1);
    }
    if (flags.json) {
      console.log(JSON.stringify(result, null, 2));
      return;
    }
    console.log(`Enabled: ${result.name}`);
    return;
  }

  if (sub === "pin") {
    if (!name) {
      console.error("Usage: capint skill pin <name>");
      process.exit(1);
    }
    const { pinSkill } = require("../lib/registry");
    const result = pinSkill(rootDir, name);
    if (result.error) {
      console.error(result.error);
      process.exit(1);
    }
    if (flags.json) {
      console.log(JSON.stringify(result, null, 2));
      return;
    }
    if (result.already) {
      console.log(`Already pinned: ${result.name}`);
    } else {
      console.log(`Pinned: ${result.name} → registry.project_added`);
      console.log(`Backup: ${path.relative(rootDir, result.backup)}`);
    }
    return;
  }

  console.error(`Unknown skill subcommand: ${sub}`);
  process.exit(1);
};
