// tools/register-hooks-tools.ts — Registers manage_hooks + SPEC-130 hook engine tools (SPEC-071, SPEC-130)
import { z } from 'zod';
import { safeLicensed } from './safe-handler.js';
import { handleManageHooks } from './manage-hooks.js';
import { handleStartHooks, handleStopHooks, handleHookStatus, handleConfigureHooks, } from './start-hooks.js';
export function registerHooksTools(server) {
    // ----- SPEC-130: Hook Engine Tools -----
    server.registerTool('start_hooks', {
        description: 'Start the file watcher and hook engine for a project. ' +
            'Idempotent — if already running, returns current status. ' +
            'Watches for file changes and dispatches to registered hook handlers.',
        inputSchema: {
            projectPath: z
                .string()
                .max(1000)
                .describe('Absolute path to the project root directory to watch'),
            hooks: z
                .array(z.string().max(100))
                .optional()
                .describe('Hook types to enable. Valid values: on-save | on-create | on-delete | on-test-pass | on-commit. ' +
                'Defaults to [on-save, on-create, on-delete]'),
        },
        annotations: {
            title: 'Start Hook Engine',
            readOnlyHint: false,
        },
    }, safeLicensed('start_hooks', async (args) => handleStartHooks(args)));
    server.registerTool('stop_hooks', {
        description: 'Stop the file watcher and hook engine. Cleans up all resources including ' +
            'watchers, timers, and event listeners.',
        inputSchema: {
            reason: z
                .string()
                .max(500)
                .optional()
                .describe('Optional reason for stopping the hook engine'),
        },
        annotations: {
            title: 'Stop Hook Engine',
            readOnlyHint: false,
        },
    }, safeLicensed('stop_hooks', async (args) => handleStopHooks(args)));
    server.registerTool('hook_status', {
        description: 'Get the current status of the hook engine including running state, ' +
            'active hooks, pending events, telemetry (execution count, avg latency, error rate), ' +
            'and event history count.',
        inputSchema: {},
        annotations: {
            title: 'Hook Engine Status',
            readOnlyHint: true,
        },
    }, safeLicensed('hook_status', async () => handleHookStatus()));
    server.registerTool('configure_hooks', {
        description: 'View or update hook engine configuration at runtime. ' +
            'If the engine is running, reloads config from .planu/hooks.json. ' +
            'If not running, shows current config from disk.',
        inputSchema: {
            projectPath: z.string().max(1000).describe('Absolute path to the project root directory'),
            enabled: z.boolean().optional().describe('Master switch for all hooks (true = enabled)'),
            debounceMs: z
                .number()
                .min(50)
                .max(10000)
                .optional()
                .describe('Debounce window in milliseconds (50-10000, default: 300)'),
            rateLimitPerFileMs: z
                .number()
                .min(1000)
                .max(60000)
                .optional()
                .describe('Rate limit per file in milliseconds (1000-60000, default: 5000)'),
            hooks: z
                .record(z.string().max(100), z.object({
                enabled: z.boolean().optional().describe('Whether this hook type is enabled'),
                include: z.array(z.string().max(500)).optional().describe('Glob patterns to include'),
                exclude: z.array(z.string().max(500)).optional().describe('Glob patterns to exclude'),
            }))
                .optional()
                .describe('Per-hook-type configuration overrides'),
        },
        annotations: {
            title: 'Configure Hooks',
            readOnlyHint: false,
        },
    }, safeLicensed('configure_hooks', async (args) => handleConfigureHooks(args)));
    // ----- SPEC-071: manage_hooks -----
    server.registerTool('manage_hooks', {
        description: 'Create, list, delete, enable/disable, and install templates for agent hooks. ' +
            'Hooks automate actions when Planu events fire (spec created, status change, drift detected, commit). ' +
            'Use dry_run to preview which hooks would fire without side effects.',
        inputSchema: {
            projectId: z.string().max(500).describe('Project ID to manage hooks for'),
            operation: z
                .enum([
                'create',
                'list',
                'delete',
                'enable',
                'disable',
                'install_template',
                'dry_run',
                'list_logs',
            ])
                .describe('Operation: create | list | delete | enable | disable | install_template | dry_run | list_logs'),
            hook: z
                .object({
                name: z.string().max(500).describe('Human-readable hook name').optional(),
                description: z.string().max(10_000).describe('What this hook does').optional(),
                trigger: z
                    .enum(['on_spec_created', 'on_spec_status_change', 'on_drift_detected', 'on_commit'])
                    .describe('Trigger: on_spec_created | on_spec_status_change | on_drift_detected | on_commit')
                    .optional(),
                filter: z
                    .object({
                    fromStatus: z
                        .string()
                        .max(500)
                        .optional()
                        .describe('Only fire when status changes FROM this'),
                    toStatus: z
                        .string()
                        .max(500)
                        .optional()
                        .describe('Only fire when status changes TO this'),
                    specIdPattern: z
                        .string()
                        .max(500)
                        .optional()
                        .describe('Only fire for specs matching this glob pattern (e.g. SPEC-*)'),
                })
                    .optional()
                    .describe('Optional filter conditions for conditional firing'),
                actions: z
                    .array(z.object({
                    type: z
                        .enum(['run_tool', 'log_message', 'set_variable', 'notify'])
                        .describe('Action type: run_tool | log_message | set_variable | notify'),
                    tool: z.string().max(500).optional().describe('Tool name (for run_tool)'),
                    args: z
                        .record(z.string().max(500), z.unknown())
                        .optional()
                        .describe('Tool args (for run_tool)'),
                    message: z
                        .string()
                        .max(10_000)
                        .optional()
                        .describe('Message template (for log_message/notify)'),
                    key: z.string().max(500).optional().describe('Variable key (for set_variable)'),
                    value: z
                        .string()
                        .max(10_000)
                        .optional()
                        .describe('Variable value (for set_variable)'),
                }))
                    .max(100)
                    .optional()
                    .describe('Ordered list of actions to execute when hook fires'),
                enabled: z.boolean().optional().describe('Whether hook is active (default: true)'),
            })
                .optional()
                .describe('Hook definition — required for create operation'),
            hookId: z
                .string()
                .max(500)
                .optional()
                .describe('Hook ID — required for delete, enable, disable, dry_run'),
            templateName: z
                .string()
                .max(500)
                .optional()
                .describe('Template name — required for install_template. Options: sdd-full | ci-ready | quality-gate'),
            dryRunPayload: z
                .object({
                trigger: z
                    .enum(['on_spec_created', 'on_spec_status_change', 'on_drift_detected', 'on_commit'])
                    .describe('Trigger type to simulate'),
                projectId: z.string().max(500).describe('Project ID for the simulated event'),
                timestamp: z.string().max(500).optional().describe('Event timestamp (defaults to now)'),
                specId: z.string().max(500).optional().describe('Spec ID (for spec-related triggers)'),
                specTitle: z.string().max(500).optional().describe('Spec title'),
                specType: z.string().max(500).optional().describe('Spec type (for on_spec_created)'),
                fromStatus: z
                    .string()
                    .max(500)
                    .optional()
                    .describe('Previous status (for on_spec_status_change)'),
                toStatus: z
                    .string()
                    .max(500)
                    .optional()
                    .describe('New status (for on_spec_status_change)'),
                driftScore: z.number().optional().describe('Drift score 0-100 (for on_drift_detected)'),
                driftSummary: z
                    .string()
                    .max(10_000)
                    .optional()
                    .describe('Drift summary (for on_drift_detected)'),
                commitHash: z.string().max(500).optional().describe('Commit hash (for on_commit)'),
                commitMessage: z
                    .string()
                    .max(10_000)
                    .optional()
                    .describe('Commit message (for on_commit)'),
                branch: z.string().max(500).optional().describe('Branch name (for on_commit)'),
            })
                .optional()
                .describe('Simulated event payload for dry_run operation'),
        },
        annotations: {
            title: 'Manage Agent Hooks',
            readOnlyHint: false,
        },
    }, safeLicensed('manage_hooks', async (args) => handleManageHooks(args)));
}
//# sourceMappingURL=register-hooks-tools.js.map