#!/usr/bin/env bash
# Run deterministic latency/performance contracts and publish gate metrics.
set -euo pipefail

ROOT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)
source "${ROOT_DIR}/scripts/ci/headless_run.sh"
REPORT_DIR="${ROOT_DIR}/target/performance-gate"
TEST_LOG="${REPORT_DIR}/cargo-test.log"
METRICS_FILE="${REPORT_DIR}/metrics.prom"
PUSHGATEWAY_URL=${QUALITY_GATE_PUSHGATEWAY_URL:-}
PUSHGATEWAY_JOB=${LESAVKA_PERFORMANCE_GATE_PUSHGATEWAY_JOB:-platform-quality-ci}

mkdir -p "${REPORT_DIR}"
cd "${ROOT_DIR}"

branch=${BRANCH_NAME:-${GIT_BRANCH:-}}
if [[ -z "${branch}" ]]; then
  branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo unknown)
fi
commit=${GIT_COMMIT:-}
if [[ -z "${commit}" ]]; then
  commit=$(git rev-parse --short HEAD 2>/dev/null || echo unknown)
fi

PERFORMANCE_TESTS=(
  --test client_uplink_performance_contract
  --test client_uplink_freshness_contract
  --test client_log_noise_contract
  --test video_downstream_feed_contract
  --test client_app_include_contract
)

start_seconds=$(date +%s)
status=0
set +e
lesavka_ci_run_headless cargo test -p lesavka_tests "${PERFORMANCE_TESTS[@]}" --color never 2>&1 | tee "${TEST_LOG}"
status=${PIPESTATUS[0]}
set -e
duration_seconds=$(($(date +%s) - start_seconds))

python3 - "${METRICS_FILE}" "${status}" "${duration_seconds}" "${branch}" "${commit}" <<'PY'
import pathlib
import sys

metrics_path = pathlib.Path(sys.argv[1])
status = int(sys.argv[2])
duration_seconds = int(sys.argv[3])
branch = sys.argv[4]
commit = sys.argv[5]

def esc(value: str) -> str:
    return value.replace('\\', r'\\').replace('\n', r'\n').replace('"', r'\"')

labels = f'suite="lesavka",branch="{esc(branch)}",commit="{esc(commit)}"'
ok = 1 if status == 0 else 0
failed = 0 if status == 0 else 1
metrics = [
    '# HELP platform_quality_gate_checks_total Check outcomes from the latest lesavka gate run.',
    '# TYPE platform_quality_gate_checks_total gauge',
    f'platform_quality_gate_checks_total{{{labels},check="performance",status="ok"}} {ok}',
    f'platform_quality_gate_checks_total{{{labels},check="performance",status="failed"}} {failed}',
    '# HELP lesavka_performance_gate_duration_seconds Runtime of the deterministic performance gate.',
    '# TYPE lesavka_performance_gate_duration_seconds gauge',
    f'lesavka_performance_gate_duration_seconds{{{labels}}} {duration_seconds}',
]
metrics_path.write_text('\n'.join(metrics) + '\n', encoding='utf-8')
PY

if [[ -n "${PUSHGATEWAY_URL}" ]]; then
  curl --fail --silent --show-error \
    --request PUT \
    --data-binary @"${METRICS_FILE}" \
    "${PUSHGATEWAY_URL%/}/metrics/job/${PUSHGATEWAY_JOB}/suite/lesavka/gate/performance" || status=$?
fi

exit "${status}"
