#!/usr/bin/env bash
# Quickstart step 3 — review the inferred schema contract and approve it.
set -euo pipefail

: "${TESSERA_API_BASE:?e.g. https://api.staging.tessera.zone/v1}"
: "${TESSERA_API_KEY:?your tsk_test_… key}"
AUTH=(-H "Authorization: Bearer $TESSERA_API_KEY")

# Infer a draft from everything profiled so far (the upload + your streamed
# events). The draft applies deny-by-default: unreviewed attributes never
# become model inputs.
DRAFT="$(curl -sS -X POST "$TESSERA_API_BASE/contracts" "${AUTH[@]}" \
  -H "Idempotency-Key: quickstart-contract-draft")"
VERSION="$(printf '%s' "$DRAFT" | python3 -c 'import json,sys; print(json.load(sys.stdin)["version"])')"

# Review what the model would see: event types and per-attribute roles.
printf '%s' "$DRAFT" | python3 -c '
import json, sys
contract = json.load(sys.stdin)
print("draft contract v%s: %s" % (contract["version"], contract["status"]))
for event_type, section in sorted(contract["event_types"].items()):
    roles = {key: spec["role"] for key, spec in section["attributes"].items()}
    print("  %s [%s]: %s" % (event_type, section["status"], json.dumps(roles, sort_keys=True)))
'

# Approve it. Datasets and serving bundles will pin this exact version.
APPROVED="$(curl -sS -X POST "$TESSERA_API_BASE/contracts/$VERSION:approve" "${AUTH[@]}")"
printf '%s' "$APPROVED" | python3 -c '
import json, sys
contract = json.load(sys.stdin)
print(json.dumps({k: contract[k] for k in ("version", "status", "fingerprint")}, indent=2))
'
