#!/usr/bin/env bash
# Refresh the committed unit-test fixtures (tests/fixtures/*.json) by capturing the
# real payloads a live backend returns for a known city. Run when the backend's content
# contract changes. Brings the backend harness up, saves each api_client endpoint's
# response, and tears it down. Requires jq for pretty output.
#
#   scripts/capture-fixtures.sh
set -euo pipefail
cd "$(dirname "$0")/.."

SLUG="${FIXTURE_CITY:-london-england-gb}"   # GB city -> Celsius rendering
MONTH="${FIXTURE_MONTH:-july}"
OUT=tests/fixtures
mkdir -p "$OUT"

BASE=$(scripts/backend-for-tests.sh up)
trap 'scripts/backend-for-tests.sh down >/dev/null 2>&1 || true' EXIT

fetch() {  # <endpoint-path> <fixture-name>
  echo "==> $2.json  <-  $1"
  curl -fsS "$BASE/$1" --max-time 40 | { if command -v jq >/dev/null; then jq .; else cat; fi; } > "$OUT/$2.json"
}

fetch "api/v2/content/home"                          home
fetch "api/v2/content/sitemap"                       sitemap
fetch "api/v2/content/hub"                           hub
fetch "api/v2/content/city/$SLUG"                    city
fetch "api/v2/content/city/$SLUG/month/$MONTH"       month
fetch "api/v2/content/city/$SLUG/records"            records

echo "==> captured $(find "$OUT" -maxdepth 1 -name '*.json' | wc -l) fixtures for $SLUG into $OUT/"
