#!/usr/bin/env bash
# Label-free quickstart step 2 — dry-run a derived label rule on your own events.
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")

# "For each anchor moment, did the user place NO order in the following
# 30 days?" Nothing is persisted: the preview samples your latest committed
# events and reports anchor counts, censoring, the positive rate and examples.
PREVIEW="$(curl -sS -X POST "$TESSERA_API_BASE/labels:preview" "${AUTH[@]}" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: label-free-preview" \
  -d '{
    "entity_type": "user",
    "task": "binary",
    "source": {
      "kind": "derived",
      "rule": {
        "template": "churn",
        "horizon_days": 30,
        "predicate": {"event_type_pattern": "order.*", "min_count": 1},
        "outcome": "absence",
        "anchor_cadence": "daily_last_event"
      }
    }
  }')"
printf '%s' "$PREVIEW" | python3 -m json.tool
printf '%s' "$PREVIEW" | python3 -c '
import json, sys
preview = json.load(sys.stdin)
print("sampled entities:", preview["sampled_entities"])
print("observations:", preview["observations"])
print("warnings:", preview["warnings"])
'
