#!/bin/bash

# UserPromptSubmit Hook
# Triggered when user submits a prompt in Claude Code
# Captures user prompts and sends to mobile app

# Source common utilities
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/common.sh"

log "INFO" "UserPromptSubmit hook triggered"

# Read JSON input from stdin
INPUT=$(read_json_input)

log "DEBUG" "Input: $INPUT"

# Extract session ID for dynamic port lookup
SESSION_ID=$(echo "$INPUT" | jq -r '.session_id // empty')

# Send to MCP server
send_to_mcp "event" "$INPUT" "$SESSION_ID"

EXIT_CODE=$?

if [ $EXIT_CODE -eq 0 ]; then
  log "INFO" "UserPromptSubmit event sent successfully"
else
  log "ERROR" "Failed to send UserPromptSubmit event (exit code: $EXIT_CODE)"
fi

exit $EXIT_CODE
