#!/usr/bin/env bash
# Script to add HTTP request metrics to handler functions
# Usage: ./scripts/add_http_metrics.sh

set -euo pipefail

# Target handlers that need metrics
HANDLERS=(
    "crates/stemedb-api/src/handlers/vote.rs:create_vote:POST:/v1/vote"
    "crates/stemedb-api/src/handlers/supersession.rs:supersede:POST:/v1/supersede"
    "crates/stemedb-api/src/handlers/epoch.rs:create_epoch:POST:/v1/epoch"
    "crates/stemedb-api/src/handlers/source.rs:store_source:POST:/v1/source"
    "crates/stemedb-api/src/handlers/source.rs:get_provenance:GET:/v1/source/provenance"
    "crates/stemedb-api/src/handlers/admin.rs:decay_trust_ranks:POST:/v1/admin/decay_trust_ranks"
    "crates/stemedb-api/src/handlers/escalation.rs:resolve_escalation:POST:/v1/admin/escalation/resolve"
    "crates/stemedb-api/src/handlers/gold_standard.rs:create_gold_standard:POST:/v1/gold_standard"
    "crates/stemedb-api/src/handlers/gold_standard.rs:remove_gold_standard:DELETE:/v1/gold_standard"
    "crates/stemedb-api/src/handlers/gold_standard.rs:verify_agent:POST:/v1/gold_standard/verify"
    "crates/stemedb-api/src/handlers/quarantine.rs:approve_quarantine:POST:/v1/admin/quarantine/approve"
    "crates/stemedb-api/src/handlers/quarantine.rs:reject_quarantine:POST:/v1/admin/quarantine/reject"
    "crates/stemedb-api/src/handlers/circuit_breaker.rs:reset_circuit:POST:/v1/admin/circuit_breaker/reset"
    "crates/stemedb-api/src/handlers/api_keys.rs:create_api_key:POST:/v1/admin/api_keys"
    "crates/stemedb-api/src/handlers/api_keys.rs:revoke_api_key:DELETE:/v1/admin/api_keys"
    "crates/stemedb-api/src/handlers/api_keys.rs:rotate_api_key:POST:/v1/admin/api_keys/rotate"
    "crates/stemedb-api/src/handlers/api_keys.rs:update_api_key:PATCH:/v1/admin/api_keys"
    "crates/stemedb-api/src/handlers/audit.rs:list_audits:GET:/v1/audit"
    "crates/stemedb-api/src/handlers/audit.rs:get_audit:GET:/v1/audit/{id}"
    "crates/stemedb-api/src/handlers/concepts.rs:resolve_alias:GET:/v1/concepts/alias"
    "crates/stemedb-api/src/handlers/concepts.rs:list_aliases:GET:/v1/concepts/aliases"
    "crates/stemedb-api/src/handlers/concepts.rs:suggest_aliases:GET:/v1/concepts/suggest"
    "crates/stemedb-api/src/handlers/concepts.rs:parse_concept_path:GET:/v1/concepts/parse"
)

echo "Adding HTTP metrics to handlers..."
echo "Pattern to add:"
echo ""
echo "  let start = std::time::Instant::now();"
echo "  metrics::counter!(\"stemedb_http_requests_total\", \"method\" => \"METHOD\", \"path\" => \"PATH\").increment(1);"
echo "  // ... handler logic ..."
echo "  let status = match &result { Ok((s, _)) => s.as_u16(), Err(_) => 500 };"
echo "  metrics::histogram!(\"stemedb_http_request_duration_seconds\","
echo "      \"method\" => \"METHOD\","
echo "      \"path\" => \"PATH\","
echo "      \"status\" => status.to_string().as_str()"
echo "  ).record(start.elapsed().as_secs_f64());"
echo ""
echo "This script provides a guide for adding metrics manually to each handler."
echo "For automated addition, use a code generation tool or apply edits systematically."
echo ""
echo "Handlers requiring metrics:"
for handler in "${HANDLERS[@]}"; do
    IFS=':' read -r file func method path <<< "$handler"
    echo "  - $file::$func ($method $path)"
done
