#!/bin/bash

# Common utilities for CodeVibe hooks
# This file provides shared functions for all hook scripts

# Configuration
# Use TMPDIR if set (macOS sets this to user-specific temp), otherwise /tmp
CODEVIBE_TMPDIR="${TMPDIR:-/tmp}"
LOG_FILE="${LOG_FILE:-${CODEVIBE_TMPDIR}/codevibe-claude-hooks.log}"
DEFAULT_PORT="${DEFAULT_PORT:-3456}"

# Log a message with timestamp
log() {
  local level="$1"
  shift
  local message="$*"
  echo "[$(date -u +"%Y-%m-%dT%H:%M:%SZ")] [$level] $message" >> "$LOG_FILE"
}

# Get the MCP server URL for a given session
# Uses session-specific port file if available, otherwise falls back to default
get_mcp_server_url() {
  local session_id="$1"
  local port="$DEFAULT_PORT"

  # Try to read session-specific port file
  if [ -n "$session_id" ]; then
    local port_file="${CODEVIBE_TMPDIR}/codevibe-claude-${session_id}.port"
    if [ -f "$port_file" ]; then
      port=$(cat "$port_file")
      log "DEBUG" "Using port from file $port_file: $port"
    else
      log "DEBUG" "Port file not found: $port_file, using default port $port"
    fi
  fi

  echo "http://localhost:${port}"
}

# Read JSON input from stdin
read_json_input() {
  # Read all input from stdin
  cat
}

# Send event to MCP server via HTTP POST
# Usage: send_to_mcp <endpoint> <json_data> [session_id]
send_to_mcp() {
  local endpoint="$1"
  local json_data="$2"
  local session_id="$3"

  # Get the MCP server URL for this session
  local mcp_url=$(get_mcp_server_url "$session_id")

  log "INFO" "Sending to MCP server: POST $mcp_url/$endpoint"
  log "DEBUG" "Payload: $json_data"

  # Send HTTP POST request to MCP server
  local response
  local http_code

  response=$(curl -s -w "\n%{http_code}" \
    -X POST \
    -H "Content-Type: application/json" \
    -d "$json_data" \
    "$mcp_url/$endpoint" 2>&1)

  http_code=$(echo "$response" | tail -n 1)
  # Use sed instead of head -n -1 for macOS compatibility
  local body=$(echo "$response" | sed '$d')

  if [ "$http_code" = "200" ]; then
    log "INFO" "Successfully sent to MCP server (HTTP $http_code)"
    log "DEBUG" "Response: $body"
    return 0
  else
    log "ERROR" "Failed to send to MCP server (HTTP $http_code)"
    log "ERROR" "Response: $body"
    return 1
  fi
}

# Extract field from JSON using grep/sed (simple alternative to jq)
# This is a basic implementation - for complex JSON, consider using jq
extract_field() {
  local json="$1"
  local field="$2"

  # Simple regex-based extraction (works for simple string values)
  echo "$json" | grep -o "\"$field\"[[:space:]]*:[[:space:]]*\"[^\"]*\"" | sed "s/\"$field\"[[:space:]]*:[[:space:]]*\"\([^\"]*\)\"/\1/"
}

# Check if MCP server is running
# Usage: check_mcp_server [session_id]
check_mcp_server() {
  local session_id="$1"
  local mcp_url=$(get_mcp_server_url "$session_id")

  log "DEBUG" "Checking if MCP server is running at $mcp_url"

  local response
  response=$(curl -s "$mcp_url/health" 2>&1)

  if [ $? -eq 0 ]; then
    log "DEBUG" "MCP server is running"
    return 0
  else
    log "WARN" "MCP server is not responding at $mcp_url"
    return 1
  fi
}

# Export variables and functions for use in other scripts
export CODEVIBE_TMPDIR
export -f log
export -f get_mcp_server_url
export -f read_json_input
export -f send_to_mcp
export -f extract_field
export -f check_mcp_server
