#!/bin/bash
# User Prompt Submit Hook for Claude Code Monitor
# Captures user prompt submission events

# Read JSON from stdin
INPUT=$(cat)

# Extract fields using jq
SESSION_ID=$(echo "$INPUT" | jq -r '.session_id // "unknown"')
PROMPT_LENGTH=$(echo "$INPUT" | jq -r '.prompt_length // 0')
TIMESTAMP=$(echo "$INPUT" | jq -r '.timestamp // (now | tonumber)')

# Build payload for backend
PAYLOAD=$(jq -n \
  --arg session_id "$SESSION_ID" \
  --arg event_type "UserPromptSubmit" \
  --arg prompt_length "$PROMPT_LENGTH" \
  --arg timestamp "$TIMESTAMP" \
  '{
    session_id: $session_id,
    event_type: $event_type,
    description: ("User submitted prompt (" + $prompt_length + " chars)"),
    timestamp: ($timestamp | tonumber)
  }')

# Send to backend API (asynchronous, non-blocking)
curl -X POST \
  -H "Content-Type: application/json" \
  -d "$PAYLOAD" \
  http://localhost:8000/api/events \
  --max-time 2 \
  --silent \
  --show-error \
  > /dev/null 2>&1 &

# Exit immediately (don't wait for curl)
exit 0
