// tools/spec-hooks-handler.ts — SPEC-368: Agent Hooks Automáticos
import { dispatchSpecHooks, describeHookActions } from '../engine/spec-hooks-engine.js';
import { addSpecHook, loadSpecHooks, getSpecHook, updateSpecHookEnabled, } from '../storage/spec-hooks-store.js';
export async function handleConfigureSpecHooks(input) {
    const { projectPath, hookName, event, action, filter, enabled = true } = input;
    await addSpecHook(projectPath, {
        name: hookName,
        event,
        action,
        filter,
        enabled,
        runCount: 0,
        failCount: 0,
        createdAt: new Date().toISOString(),
    });
    const filterDesc = filter ? ` (filter: ${JSON.stringify(filter)})` : '';
    return {
        content: [
            {
                type: 'text',
                text: [
                    `Hook configured: "${hookName}"`,
                    `  Event: ${event}`,
                    `  Action: ${action}${filterDesc}`,
                    `  Status: ${enabled ? 'enabled' : 'disabled'}`,
                ].join('\n'),
            },
        ],
    };
}
export async function handleListSpecHooks(input) {
    const config = await loadSpecHooks(input.projectPath);
    if (config.hooks.length === 0) {
        return {
            content: [
                { type: 'text', text: 'No spec hooks configured. Use configure_spec_hooks to add one.' },
            ],
        };
    }
    const lines = config.hooks.map((h) => {
        const status = h.enabled ? 'enabled' : 'disabled';
        const stats = `runs: ${h.runCount}, fails: ${h.failCount}`;
        const last = h.lastRunAt ? ` last: ${h.lastRunAt}` : '';
        return `• ${h.name} [${status}] — ${h.event} → ${h.action} (${stats}${last})`;
    });
    return {
        content: [
            {
                type: 'text',
                text: `Spec hooks (${config.hooks.length}):\n${lines.join('\n')}`,
            },
        ],
    };
}
export async function handleTestSpecHook(input) {
    const { projectPath, hookName, specId } = input;
    const hook = await getSpecHook(projectPath, hookName);
    if (!hook) {
        return {
            content: [{ type: 'text', text: `Hook "${hookName}" not found.` }],
            isError: true,
        };
    }
    const config = await loadSpecHooks(projectPath);
    const result = dispatchSpecHooks({
        event: hook.event,
        specId,
        projectPath,
    }, config);
    const actionDesc = describeHookActions(hook, specId);
    const lines = [
        `Test simulation for hook "${hookName}" on spec ${specId}:`,
        ``,
        `Event: ${hook.event}`,
        `Action: ${hook.action}`,
        `Would trigger: ${result.triggered.includes(hookName) ? 'YES' : 'NO (disabled or filter mismatch)'}`,
        ``,
        `If triggered, would execute:`,
        `  ${actionDesc}`,
        ``,
        `Dispatch summary:`,
        result.description,
    ];
    return {
        content: [{ type: 'text', text: lines.join('\n') }],
    };
}
export async function handleDisableSpecHook(input) {
    const updated = await updateSpecHookEnabled(input.projectPath, input.hookName, false);
    if (!updated) {
        return {
            content: [{ type: 'text', text: `Hook "${input.hookName}" not found.` }],
            isError: true,
        };
    }
    return {
        content: [{ type: 'text', text: `Hook "${input.hookName}" disabled.` }],
    };
}
export async function handleEnableSpecHook(input) {
    const updated = await updateSpecHookEnabled(input.projectPath, input.hookName, true);
    if (!updated) {
        return {
            content: [{ type: 'text', text: `Hook "${input.hookName}" not found.` }],
            isError: true,
        };
    }
    return {
        content: [{ type: 'text', text: `Hook "${input.hookName}" enabled.` }],
    };
}
//# sourceMappingURL=spec-hooks-handler.js.map