/**
 * `olam skills install-hook [--scope project|user]` — install the
 * SessionStart hook that runs `olam skills sync --quiet` on every
 * Claude Code session start.
 *
 * `olam skills uninstall-hook [--scope project|user]` — surgical
 * sentinel-matched removal.
 *
 * Mirrors `olam kg install-hook` / `olam kg uninstall-hook` shape:
 *   - `--scope project` (default) → <cwd>/.claude/settings.json
 *   - `--scope user`              → ~/.claude/settings.json
 *
 * Idempotent install via OLAM_SKILLS_HOOK_SENTINEL substring-match.
 * Surgical uninstall: only entries whose command contains the sentinel
 * are dropped; user-authored hooks are preserved.
 */
import { installSkillsHookToFile, skillsHookSettingsPathFor, uninstallSkillsHookFromFile, } from '@olam/core/src/skill-sources/index.js';
import * as fs from 'node:fs';
import { printError, printInfo, printSuccess, printWarning } from '../output.js';
function backup(filePath) {
    if (!fs.existsSync(filePath))
        return null;
    const ts = new Date().toISOString().replace(/[:.]/g, '-');
    const backupPath = `${filePath}.olam-bak.${ts}`;
    fs.copyFileSync(filePath, backupPath);
    return backupPath;
}
export function registerSkillsHook(program) {
    const skills = program.commands.find((c) => c.name() === 'skills') ??
        program.command('skills').description('Manage skill sources and synchronization');
    skills
        .command('install-hook')
        .description('Install the SessionStart hook that runs `olam skills sync --quiet` on session start')
        .option('--scope <scope>', 'project (default; <cwd>/.claude/settings.json) or user (~/.claude/settings.json)', 'project')
        .action((opts) => {
        const scope = opts.scope === 'user' ? 'user' : 'project';
        const filePath = skillsHookSettingsPathFor(scope);
        try {
            const result = installSkillsHookToFile(filePath);
            switch (result.status) {
                case 'installed':
                    printSuccess(`olam-skills SessionStart hook installed (${scope} scope)`);
                    printInfo('settings', filePath);
                    if (result.backupPath)
                        printInfo('backup', result.backupPath);
                    printInfo('next', 'open a new Claude Code session; `olam skills sync --quiet` will run on start');
                    return;
                case 'already-present':
                    printInfo('olam-skills hook', `already installed at ${filePath}`);
                    return;
                case 'no-op':
                    printWarning(`no change made to ${filePath}`);
                    return;
            }
        }
        catch (err) {
            printError(`install-hook failed: ${err instanceof Error ? err.message : String(err)}`);
            process.exitCode = 1;
        }
    });
    skills
        .command('uninstall-hook')
        .description('Remove the olam-skills SessionStart hook from settings.json (sentinel-matched; surgical)')
        .option('--scope <scope>', 'project (default) or user', 'project')
        .action((opts) => {
        const scope = opts.scope === 'user' ? 'user' : 'project';
        const filePath = skillsHookSettingsPathFor(scope);
        const backupPath = backup(filePath);
        try {
            const result = uninstallSkillsHookFromFile(filePath);
            switch (result.status) {
                case 'removed':
                    printSuccess(`olam-skills SessionStart hook removed from ${filePath}`);
                    if (backupPath)
                        printInfo('backup', backupPath);
                    return;
                case 'not-found':
                    printInfo('olam-skills hook', `not found in ${filePath} — already uninstalled`);
                    if (backupPath) {
                        try {
                            fs.unlinkSync(backupPath);
                        }
                        catch { /* best-effort */ }
                    }
                    return;
                case 'no-settings':
                    printInfo('olam-skills hook', `no settings.json at ${filePath} — nothing to remove`);
                    return;
            }
        }
        catch (err) {
            printError(`uninstall-hook failed: ${err instanceof Error ? err.message : String(err)}`);
            if (backupPath)
                printInfo('backup', `settings.json preserved at ${backupPath}`);
            process.exitCode = 1;
        }
    });
}
//# sourceMappingURL=skills-hook.js.map