#!/usr/bin/env bash
# Quickstart step 4 — define the binary label and upload its observations.
set -euo pipefail

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

# --- 1. a source for the label table -----------------------------------------
LABEL_SOURCE="$(curl -sS -X POST "$TESSERA_API_BASE/sources" "${AUTH[@]}" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: quickstart-label-source" \
  -d '{"name": "quickstart-labels", "kind": "file_upload", "config": {}}')"
LABEL_SOURCE_ID="$(printf '%s' "$LABEL_SOURCE" | python3 -c 'import json,sys; print(json.load(sys.stdin)["source_id"])')"

# --- 2. the label definition --------------------------------------------------
# `observed_at` is when each outcome became knowable; `maturation_days: 7`
# masks any supervision where the entity was active within 7 days of the
# observation — the model must genuinely predict, never read back.
LABEL="$(curl -sS -X POST "$TESSERA_API_BASE/labels" "${AUTH[@]}" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: quickstart-label" \
  -d '{
    "name": "repeat_purchase_30d",
    "entity_type": "user",
    "task": "binary",
    "source": {
      "kind": "label_table",
      "source_id": "'"$LABEL_SOURCE_ID"'",
      "entity_id_column": "entity_id",
      "value_column": "value",
      "observed_at_column": "observed_at"
    },
    "maturation_days": 7
  }')"
LABEL_ID="$(printf '%s' "$LABEL" | python3 -c 'import json,sys; print(json.load(sys.stdin)["label_id"])')"
printf '%s' "$LABEL" | python3 -c '
import json, sys
label = json.load(sys.stdin)
print(json.dumps({k: label[k] for k in ("label_id", "name", "entity_type", "task", "maturation_days")}, indent=2))
'

# --- 3. upload the observations to that source --------------------------------
SESSION="$(curl -sS -X POST "$TESSERA_API_BASE/uploads" "${AUTH[@]}" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: quickstart-upload-labels" \
  -d '{"filename": "labels_repeat_purchase.csv", "format": "csv", "source_id": "'"$LABEL_SOURCE_ID"'"}')"
UPLOAD_ID="$(printf '%s' "$SESSION" | python3 -c 'import json,sys; print(json.load(sys.stdin)["upload_id"])')"
UPLOAD_URL="$(printf '%s' "$SESSION" | python3 -c 'import json,sys; print(json.load(sys.stdin)["upload_url"])')"
curl -sS -X PUT "$UPLOAD_URL" --data-binary "@$SAMPLE_DIR/labels_repeat_purchase.csv" > /dev/null
curl -sS -X POST "$TESSERA_API_BASE/uploads/$UPLOAD_ID:complete" "${AUTH[@]}" > /dev/null

LABEL_SYNC_ID="$(curl -sS "$TESSERA_API_BASE/uploads/$UPLOAD_ID" "${AUTH[@]}" \
  | python3 -c 'import json,sys; print(json.load(sys.stdin)["sync_id"])')"
for _ in $(seq 1 300); do
  STATUS="$(curl -sS "$TESSERA_API_BASE/syncs/$LABEL_SYNC_ID" "${AUTH[@]}" \
    | python3 -c 'import json,sys; print(json.load(sys.stdin)["status"])')"
  if [ "$STATUS" = "succeeded" ] || [ "$STATUS" = "failed" ]; then break; fi
  sleep 1
done
echo "label sync: $STATUS"

# --- 4. sanity-check the ingested observations ---------------------------------
curl -sS "$TESSERA_API_BASE/labels/$LABEL_ID/stats" "${AUTH[@]}" | python3 -m json.tool
printf '%s' "$LABEL_ID" > "$TESSERA_STATE_DIR/label_id"
