// engine/hooks/git-hook-generator.ts — Generates shell scripts for git hooks (SPEC-071)
// Git hooks are generated as shell scripts — Planu does NOT execute them directly.
// ---------------------------------------------------------------------------
// Script templates
// ---------------------------------------------------------------------------
function buildPreCommitScript(projectId) {
    return `#!/usr/bin/env sh
# Planu pre-commit hook — auto-generated by manage_hooks
# Project: ${projectId}
# Do not edit manually — re-run manage_hooks install_template to regenerate.

set -e

echo "[Planu] Running pre-commit checks..."

# Dispatch on_commit event to Planu (fire-and-forget — does not block commit)
# Requires planu CLI to be installed: npm install -g planu (or equivalent)
if command -v planu >/dev/null 2>&1; then
  COMMIT_HASH="$(git rev-parse --short HEAD 2>/dev/null || echo 'unknown')"
  BRANCH="$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo 'unknown')"
  COMMIT_MSG="$(git log -1 --pretty=%B 2>/dev/null | head -1 || echo '')"
  planu dispatch on_commit \\
    --project-id "${projectId}" \\
    --commit-hash "\${COMMIT_HASH}" \\
    --branch "\${BRANCH}" \\
    --message "\${COMMIT_MSG}" \\
    2>/dev/null || true
fi

echo "[Planu] Pre-commit checks complete."
`;
}
function buildPostCommitScript(projectId) {
    return `#!/usr/bin/env sh
# Planu post-commit hook — auto-generated by manage_hooks
# Project: ${projectId}
# Do not edit manually — re-run manage_hooks install_template to regenerate.

echo "[Planu] Post-commit: recording commit event..."

if command -v planu >/dev/null 2>&1; then
  COMMIT_HASH="$(git rev-parse --short HEAD 2>/dev/null || echo 'unknown')"
  BRANCH="$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo 'unknown')"
  COMMIT_MSG="$(git log -1 --pretty=%B 2>/dev/null | head -1 || echo '')"
  planu dispatch on_commit \\
    --project-id "${projectId}" \\
    --commit-hash "\${COMMIT_HASH}" \\
    --branch "\${BRANCH}" \\
    --message "\${COMMIT_MSG}" \\
    2>/dev/null || true
fi
`;
}
function buildPrePushScript(projectId) {
    return `#!/usr/bin/env sh
# Planu pre-push hook — auto-generated by manage_hooks
# Project: ${projectId}
# Do not edit manually — re-run manage_hooks install_template to regenerate.

set -e

echo "[Planu] Running pre-push drift check..."

if command -v planu >/dev/null 2>&1; then
  # Run drift detection for all active specs
  planu detect-drift --project-id "${projectId}" --mode quick 2>/dev/null || true
fi

echo "[Planu] Pre-push checks complete."
`;
}
// ---------------------------------------------------------------------------
// Generator functions
// ---------------------------------------------------------------------------
/**
 * Generate a shell script for the given git hook type.
 */
export function generateGitHookScript(hookType, projectId, projectPath) {
    const installPath = `${projectPath}/.git/hooks/${hookType}`;
    let content;
    switch (hookType) {
        case 'pre-commit':
            content = buildPreCommitScript(projectId);
            break;
        case 'post-commit':
            content = buildPostCommitScript(projectId);
            break;
        case 'pre-push':
            content = buildPrePushScript(projectId);
            break;
    }
    return { hookType, content, installPath };
}
/**
 * Generate git hook scripts for all requested hook types.
 */
export function generateGitHooks(input) {
    return input.hookTypes.map((hookType) => generateGitHookScript(hookType, input.projectId, input.projectPath));
}
/**
 * Format the generated scripts as a human-readable installation guide.
 */
export function formatGitHookInstallGuide(scripts) {
    const lines = ['Git hook scripts generated. To install:', '', '```bash'];
    for (const script of scripts) {
        lines.push(`# Install ${script.hookType} hook`);
        lines.push(`cat > ${script.installPath} << 'PLANU_HOOK'`);
        lines.push(script.content.trim());
        lines.push('PLANU_HOOK');
        lines.push(`chmod +x ${script.installPath}`);
        lines.push('');
    }
    lines.push('```');
    lines.push('');
    lines.push('Note: These hooks dispatch events to Planu. They are fire-and-forget');
    lines.push('and will not block git operations if Planu is unavailable.');
    return lines.join('\n');
}
//# sourceMappingURL=git-hook-generator.js.map