/**
 * Claude command - manage Claude Code skill installation
 *
 * Usage:
 *   quickback claude install           # Interactive: choose global or local
 *   quickback claude install --global  # Install to ~/.claude/skills/quickback/
 *   quickback claude install --local   # Install to ./quickback/.claude/skills/quickback/
 *   quickback claude update            # Update to latest version
 *   quickback claude remove            # Remove installed skill
 *   quickback claude status            # Check installation status
 */
import { copyFileSync, mkdirSync, existsSync, rmSync, readdirSync, statSync } from "fs";
import { join, dirname } from "path";
import { homedir } from "os";
import { fileURLToPath } from "url";
import pc from "picocolors";
import prompts from "prompts";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// Paths
const SKILL_BUNDLE = join(__dirname, "..", "skill");
const GLOBAL_SKILLS_DIR = join(homedir(), ".claude", "skills", "quickback");
const GLOBAL_AGENTS_DIR = join(homedir(), ".claude", "agents", "quickback-specialist");
const LOCAL_SKILLS_DIR = join(process.cwd(), "quickback", ".claude", "skills", "quickback");
const LOCAL_AGENTS_DIR = join(process.cwd(), "quickback", ".claude", "agents", "quickback-specialist");
function getLocations() {
    return {
        global: {
            skillDir: GLOBAL_SKILLS_DIR,
            agentDir: GLOBAL_AGENTS_DIR,
            label: "global (~/.claude/)",
        },
        local: {
            skillDir: LOCAL_SKILLS_DIR,
            agentDir: LOCAL_AGENTS_DIR,
            label: "local (./quickback/.claude/)",
        },
    };
}
function copyDir(src, dest) {
    if (!existsSync(dest)) {
        mkdirSync(dest, { recursive: true });
    }
    const entries = readdirSync(src);
    for (const entry of entries) {
        const srcPath = join(src, entry);
        const destPath = join(dest, entry);
        if (statSync(srcPath).isDirectory()) {
            copyDir(srcPath, destPath);
        }
        else {
            copyFileSync(srcPath, destPath);
        }
    }
}
function checkBundledSkill() {
    const skillMd = join(SKILL_BUNDLE, "SKILL.md");
    if (!existsSync(skillMd)) {
        console.log(pc.red("\nSkill files not found in CLI bundle."));
        console.log(pc.dim("This may indicate an incomplete CLI installation."));
        console.log(pc.dim("Try reinstalling: npm install -g @quickback-dev/cli\n"));
        return false;
    }
    return true;
}
function installTo(location) {
    const { skillDir, agentDir, label } = location;
    // Install SKILL.md
    mkdirSync(skillDir, { recursive: true });
    const skillMdSrc = join(SKILL_BUNDLE, "SKILL.md");
    const skillMdDest = join(skillDir, "SKILL.md");
    copyFileSync(skillMdSrc, skillMdDest);
    console.log(pc.green(`  ✓ SKILL.md → ${skillDir}`));
    // Install agents
    const agentsSrc = join(SKILL_BUNDLE, "agents", "quickback-specialist");
    if (existsSync(agentsSrc)) {
        mkdirSync(agentDir, { recursive: true });
        copyDir(agentsSrc, agentDir);
        console.log(pc.green(`  ✓ AGENT.md → ${agentDir}`));
    }
    console.log("");
    console.log(pc.green(`✓ Quickback skill installed to ${label}`));
    console.log("");
    console.log(pc.bold("Restart Claude Code to activate the skill."));
    console.log(pc.dim("The skill will automatically activate when working in Quickback projects.\n"));
}
function removeFrom(location) {
    const { skillDir, agentDir } = location;
    let removed = false;
    if (existsSync(skillDir)) {
        rmSync(skillDir, { recursive: true, force: true });
        console.log(pc.yellow(`  ✓ Removed ${skillDir}`));
        removed = true;
    }
    if (existsSync(agentDir)) {
        rmSync(agentDir, { recursive: true, force: true });
        console.log(pc.yellow(`  ✓ Removed ${agentDir}`));
        removed = true;
    }
    return removed;
}
function checkInstallation() {
    const locations = getLocations();
    return {
        global: existsSync(join(locations.global.skillDir, "SKILL.md")),
        local: existsSync(join(locations.local.skillDir, "SKILL.md")),
    };
}
async function install(args) {
    if (!checkBundledSkill()) {
        process.exit(1);
    }
    const locations = getLocations();
    const installed = checkInstallation();
    // Check for flags
    const isGlobal = args.includes("--global") || args.includes("-g");
    const isLocal = args.includes("--local") || args.includes("-l");
    let targetLocation;
    if (isGlobal) {
        targetLocation = locations.global;
    }
    else if (isLocal) {
        targetLocation = locations.local;
    }
    else {
        // Interactive prompt
        console.log("");
        console.log(pc.bold("Install Quickback Skill for Claude Code"));
        console.log("");
        if (installed.global) {
            console.log(pc.dim(`  Global installation: ${pc.green("installed")}`));
        }
        if (installed.local) {
            console.log(pc.dim(`  Local installation: ${pc.green("installed")}`));
        }
        if (installed.global || installed.local) {
            console.log("");
        }
        const response = await prompts({
            type: "select",
            name: "location",
            message: "Where should the skill be installed?",
            choices: [
                {
                    title: "Global (recommended)",
                    description: "Available in all projects (~/.claude/skills/)",
                    value: "global",
                },
                {
                    title: "Local",
                    description: "Only this project (./quickback/.claude/)",
                    value: "local",
                },
            ],
        });
        if (!response.location) {
            console.log(pc.dim("\nInstallation cancelled.\n"));
            return;
        }
        targetLocation = response.location === "global" ? locations.global : locations.local;
    }
    // Check for existing installation
    const existingSkill = existsSync(join(targetLocation.skillDir, "SKILL.md"));
    if (existingSkill) {
        const response = await prompts({
            type: "confirm",
            name: "overwrite",
            message: `Skill already installed at ${targetLocation.label}. Overwrite?`,
            initial: true,
        });
        if (!response.overwrite) {
            console.log(pc.dim("\nInstallation cancelled.\n"));
            return;
        }
    }
    console.log("");
    installTo(targetLocation);
}
async function update(args) {
    if (!checkBundledSkill()) {
        process.exit(1);
    }
    const locations = getLocations();
    const installed = checkInstallation();
    if (!installed.global && !installed.local) {
        console.log(pc.yellow("\nNo existing installation found."));
        console.log(pc.dim("Run 'quickback claude install' to install.\n"));
        return;
    }
    console.log("");
    console.log(pc.bold("Updating Quickback Skill"));
    console.log("");
    if (installed.global) {
        installTo(locations.global);
    }
    if (installed.local) {
        installTo(locations.local);
    }
}
async function remove(args) {
    const locations = getLocations();
    const installed = checkInstallation();
    if (!installed.global && !installed.local) {
        console.log(pc.yellow("\nNo installation found to remove.\n"));
        return;
    }
    console.log("");
    console.log(pc.bold("Remove Quickback Skill"));
    console.log("");
    let removed = false;
    if (installed.global) {
        removed = removeFrom(locations.global) || removed;
    }
    if (installed.local) {
        removed = removeFrom(locations.local) || removed;
    }
    if (removed) {
        console.log("");
        console.log(pc.green("✓ Quickback skill removed."));
        console.log(pc.dim("Restart Claude Code to complete removal.\n"));
    }
}
async function status() {
    const installed = checkInstallation();
    const locations = getLocations();
    console.log("");
    console.log(pc.bold("Quickback Claude Code Skill"));
    console.log("");
    if (installed.global) {
        console.log(`  ${pc.green("●")} Global: ${pc.green("installed")}`);
        console.log(pc.dim(`    ${locations.global.skillDir}`));
    }
    else {
        console.log(`  ${pc.dim("○")} Global: ${pc.dim("not installed")}`);
    }
    console.log("");
    if (installed.local) {
        console.log(`  ${pc.green("●")} Local: ${pc.green("installed")}`);
        console.log(pc.dim(`    ${locations.local.skillDir}`));
    }
    else {
        console.log(`  ${pc.dim("○")} Local: ${pc.dim("not installed")}`);
    }
    console.log("");
    if (!installed.global && !installed.local) {
        console.log(pc.dim("Run 'quickback claude install' to install the skill.\n"));
    }
}
function printHelp() {
    console.log(`
${pc.bold("Quickback Claude Code Skill")}

${pc.bold("USAGE")}
  quickback claude <command> [options]

${pc.bold("COMMANDS")}
  ${pc.cyan("install")}    Install the Claude Code skill
  ${pc.cyan("update")}     Update to the latest version
  ${pc.cyan("remove")}     Remove the installed skill
  ${pc.cyan("status")}     Check installation status

${pc.bold("OPTIONS")}
  ${pc.cyan("--global, -g")}    Install to ~/.claude/skills/ (all projects)
  ${pc.cyan("--local, -l")}     Install to ./quickback/.claude/ (this project)

${pc.bold("EXAMPLES")}
  ${pc.gray("# Interactive installation")}
  quickback claude install

  ${pc.gray("# Install globally")}
  quickback claude install --global

  ${pc.gray("# Check status")}
  quickback claude status
`);
}
export async function claude(args) {
    const subcommand = args[0];
    const subArgs = args.slice(1);
    switch (subcommand) {
        case "install":
            await install(subArgs);
            break;
        case "update":
            await update(subArgs);
            break;
        case "remove":
        case "uninstall":
            await remove(subArgs);
            break;
        case "status":
            await status();
            break;
        case "--help":
        case "-h":
        case undefined:
            printHelp();
            break;
        default:
            console.log(pc.red(`\nUnknown subcommand: ${subcommand}`));
            printHelp();
            process.exit(1);
    }
}
//# sourceMappingURL=claude.js.map