#!/bin/bash
set -euo pipefail

node /dev/fd/3 3<<'EOF'
const fs = require('node:fs');

const rawInput = fs.readFileSync(0, 'utf8');
const input = parseJsonObject(rawInput);
const eventName = String(input.hook_event_name || input.hookEventName || '');
const commandName = normalizeCommandName(input.command_name || input.commandName || '');

if (eventName !== 'UserPromptExpansion' || commandName !== 'spec:plan') {
  process.exit(0);
}

const permissionMode = typeof input.permission_mode === 'string' && input.permission_mode.trim()
  ? input.permission_mode.trim()
  : 'unknown';
const permissionLine = permissionMode === 'plan'
  ? 'permission_mode=plan: Claude native Plan Mode write protection is active.'
  : `permission_mode=${permissionMode}: best-effort attention reminder only; this hook provides no hard write protection.`;

const additionalContext = [
  '[spec-first] /spec:plan planning-only attention guard',
  'This slash-command expansion is planning-only.',
  'Allowed before handoff: research, decisions, and writing or updating the plan artifact.',
  'Do not implement code, edit non-plan source files, run implementation workflows, or claim implementation has started.',
  'After the plan is written and reviewed, stop and wait for the user handoff choice before implementation.',
  permissionLine,
].join('\n');

process.stdout.write(JSON.stringify({
  hookSpecificOutput: {
    hookEventName: 'UserPromptExpansion',
    additionalContext,
  },
}));

function parseJsonObject(raw) {
  try {
    const parsed = JSON.parse(raw || '{}');
    return parsed && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed : {};
  } catch {
    return {};
  }
}

function normalizeCommandName(value) {
  return String(value || '').trim().replace(/^\//, '');
}
EOF
