#!/usr/bin/env bash
# Design-partner runbook, step 4: the lead-time backtest under the same protocol.
# Optional: only for protocols that declare lead_time_metrics. Set TESSERA_MODEL_ID.
#
# The Idempotency-Key is derived from the model, the protocol and the seed: the
# same backtest is a safe retry, and another seed is another backtest.
set -euo pipefail

: "${TESSERA_API_BASE:?e.g. https://api.staging.tessera.zone/v1}"
: "${TESSERA_API_KEY:?the tsk_ key of the partner project}"
: "${TESSERA_MODEL_ID:?the trained model, mdl_...}"
: "${TESSERA_STATE_DIR:?set by the runner; ids are handed between steps}"
AUTH=(-H "Authorization: Bearer $TESSERA_API_KEY")
PROTOCOL_ID="$(cat "$TESSERA_STATE_DIR/protocol_id")"
SEED="${TESSERA_BACKTEST_SEED:-7}"
MAX_POLLS="${TESSERA_MAX_POLLS:-720}"   # 5 s apart: one hour by default

fail() { echo "$1" >&2; exit 1; }

KEY="docs-partner-backtest-$(printf '%s:%s:%s' "$TESSERA_MODEL_ID" "$PROTOCOL_ID" "$SEED" | python3 -c '
import hashlib, sys
print(hashlib.sha256(sys.stdin.buffer.read()).hexdigest()[:24])
')"
BACKTEST="$(curl -sS --fail-with-body -X POST "$TESSERA_API_BASE/backtests" "${AUTH[@]}" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: $KEY" \
  -d '{"protocol_id": "'"$PROTOCOL_ID"'", "model_id": "'"$TESSERA_MODEL_ID"'", "seed": '"$SEED"'}')" \
  || fail "starting the backtest failed: $BACKTEST"
BACKTEST_ID="$(printf '%s' "$BACKTEST" | python3 -c 'import json,sys; print(json.load(sys.stdin)["backtest_id"])')"

STATUS=""
for _ in $(seq 1 "$MAX_POLLS"); do
  DETAIL="$(curl -sS --fail-with-body "$TESSERA_API_BASE/backtests/$BACKTEST_ID" "${AUTH[@]}")" \
    || fail "reading backtest $BACKTEST_ID failed: $DETAIL"
  STATUS="$(printf '%s' "$DETAIL" | python3 -c 'import json,sys; print(json.load(sys.stdin)["backtest"]["status"])')"
  if [ "$STATUS" = "succeeded" ] || [ "$STATUS" = "failed" ]; then break; fi
  sleep 5
done
echo "backtest: $STATUS"
if [ "$STATUS" != "succeeded" ]; then
  printf '%s' "$DETAIL" | python3 -c '
import json, sys
row = json.load(sys.stdin)["backtest"]
print("reason:", row.get("reason", "still running after the polling bound"), file=sys.stderr)
'
  exit 1
fi
printf '%s' "$DETAIL" | python3 -c '
import json, sys
body = json.load(sys.stdin)["backtest"]
print("fingerprint:", body.get("fingerprint"))
'
echo "$BACKTEST_ID" > "$TESSERA_STATE_DIR/backtest_id"
