#!/bin/bash
# BE Systematic — garage platform partner, sandbox walkthrough.
# Fictitious data only (example.test, registrations ending TST).
set -euo pipefail
HOST="${HOST:-https://hawkeye-next.brownhawke.engineering}"
BASE="$HOST/api/machine-viewer/v1"
# Optional: a Cloudflare Access service token for a host behind Access (staging answers 302 without it).
#   CF_ACCESS_CLIENT_ID=... CF_ACCESS_CLIENT_SECRET=... bash curl.sh
ACCESS=()
if [ -n "${CF_ACCESS_CLIENT_ID:-}" ] && [ -n "${CF_ACCESS_CLIENT_SECRET:-}" ]; then
  ACCESS=(-H "CF-Access-Client-Id: $CF_ACCESS_CLIENT_ID" -H "CF-Access-Client-Secret: $CF_ACCESS_CLIENT_SECRET")
fi
api() { curl -sS ${ACCESS[@]+"${ACCESS[@]}"} "$@"; }

echo "1. Mint a sandbox session"
SESSION=$(api -X POST "$BASE/connectors/garage-platform/sandbox/session" -H "Content-Type: application/json" -d '{}')
KEY=$(python3 -c "import json,sys; print(json.load(sys.stdin)['api_key'])" <<<"$SESSION")
echo "   site garage-01 key issued"

echo "2. Push a new Inspection job for AB01TST"
api -X POST "$BASE/connectors/garage-platform/jobs" \
  -H "Authorization: Bearer $KEY" -H "Content-Type: application/json" \
  -H "Idempotency-Key: apt_demo_new-create" \
  -d '{
  "appointment_id": "apt_demo_new",
  "job_number": "JS-DEMO-1",
  "workflow_stage": "Inspection",
  "priority": "Urgent",
  "appointment_date": "2026-11-16",
  "appointment_time": "09:30",
  "vehicle_registration": "AB01TST",
  "vehicle_year": "2019",
  "vehicle_make": "Ford",
  "vehicle_model": "Focus",
  "colour": "Blue",
  "fuel_type": "Petrol",
  "engine_capacity": "1.0",
  "task_description": "Engine overheating, coolant loss",
  "internal_notes": "Warning light on after a short run. Example.test demo job."
}'

echo
echo "3. Close the job so the fix is stored for the next similar vehicle"
api -X POST "$BASE/job-cards/apt_demo_new/close" \
  -H "Authorization: Bearer $KEY" -H "Content-Type: application/json" \
  -H "Idempotency-Key: apt_demo_new-close" \
  -d '{"cause":"Water pump bearing failed","rectification":"Replaced water pump and coolant","parts":["Water pump"],"engineer":"Alex Technician"}'

echo
echo "Done. Open $HOST/docs/machine-mate/garage-platform"
