#!/bin/bash
# Hook: Check for pending test generation or restructuring tasks
# Triggers: On user prompt submit
# Purpose: Remind Claude about pending tasks from previous hooks

set -euo pipefail

# Read hook input from stdin
INPUT=$(cat)

# Check for marker files indicating pending tasks
MARKER_DIR="/tmp"
TEST_MARKERS=$(find "$MARKER_DIR" -name "claude_code_needs_tests_*.marker" 2>/dev/null || true)

if [[ -n "$TEST_MARKERS" ]]; then
  # Found pending test generation tasks
  FILES_NEEDING_TESTS=""

  while IFS= read -r marker_file; do
    if [[ -f "$marker_file" ]]; then
      FILE_PATH=$(cat "$marker_file")
      FILES_NEEDING_TESTS="$FILES_NEEDING_TESTS\n  - $FILE_PATH"
      # Clean up old markers (older than 1 hour)
      find "$marker_file" -mmin +60 -delete 2>/dev/null || true
    fi
  done <<< "$TEST_MARKERS"

  if [[ -n "$FILES_NEEDING_TESTS" ]]; then
    cat <<EOF
{
  "continue": true,
  "systemMessage": "⚠️  Pending Tasks:\n\n🧪 Test Generation Needed:$FILES_NEEDING_TESTS\n\nShould I write comprehensive tests for these files to ensure reliability?",
  "hookSpecificOutput": {
    "hookEventName": "UserPromptSubmit",
    "pendingTasks": true,
    "taskType": "test-generation"
  }
}
EOF
    exit 0
  fi
fi

# No pending tasks - continue normally
cat <<EOF
{
  "continue": true,
  "suppressOutput": true
}
EOF

exit 0
