#!/usr/bin/env bash
# Quickstart step 2 — download the sample dataset, upload the e-commerce CSV,
# map its columns to canonical events, and run the first full sync.
set -euo pipefail

: "${TESSERA_API_BASE:?e.g. https://api.staging.tessera.zone/v1}"
: "${TESSERA_API_KEY:?your tsk_test_… key}"
: "${TESSERA_STATE_DIR:=.}"
# The published dataset lives on the docs site; CI points this at its stack.
SAMPLE_URL="${TESSERA_SAMPLE_URL:-https://docs.staging.tessera.zone/downloads/tessera-sample-v1.zip}"
AUTH=(-H "Authorization: Bearer $TESSERA_API_KEY")

# --- 1. download + verify the sample dataset --------------------------------
SAMPLE_DIR="$TESSERA_STATE_DIR/sample"
mkdir -p "$SAMPLE_DIR"
curl -sSL "$SAMPLE_URL" -o "$SAMPLE_DIR/tessera-sample-v1.zip"
(cd "$SAMPLE_DIR" && python3 -c '
import hashlib, json, zipfile
archive = zipfile.ZipFile("tessera-sample-v1.zip")
archive.extractall()
manifest = json.load(open("manifest.json"))
for name, meta in manifest["files"].items():
    digest = hashlib.sha256(open(name, "rb").read()).hexdigest()
    assert digest == meta["sha256"], f"checksum mismatch: {name}"
print("sample dataset verified:", ", ".join(sorted(manifest["files"])))
print("dataset fingerprint:", manifest["fingerprint"])
')

# --- 2. create an upload session and send the bytes --------------------------
SESSION="$(curl -sS -X POST "$TESSERA_API_BASE/uploads" "${AUTH[@]}" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: quickstart-upload-orders" \
  -d '{"filename": "ecommerce_orders.csv", "format": "csv"}')"
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/ecommerce_orders.csv" > /dev/null
curl -sS -X POST "$TESSERA_API_BASE/uploads/$UPLOAD_ID:complete" "${AUTH[@]}" > /dev/null

# Completing attaches the file to a `file_upload` source (status `completed`)
# and enqueues a landing sync; wait for it before mapping. Poll the upload, not
# only the first sync: if that sync fails before ingesting the file, Tessera
# hands the file to one follow-up sync, and the upload names it.
UPLOAD="$(curl -sS "$TESSERA_API_BASE/uploads/$UPLOAD_ID" "${AUTH[@]}")"
SOURCE_ID="$(printf '%s' "$UPLOAD" | python3 -c 'import json,sys; print(json.load(sys.stdin)["source_id"])')"
LANDING_SYNC_ID="$(printf '%s' "$UPLOAD" | python3 -c 'import json,sys; print(json.load(sys.stdin)["sync_id"])')"
echo "upload status: $(printf '%s' "$UPLOAD" | python3 -c 'import json,sys; print(json.load(sys.stdin)["status"])')"
for _ in $(seq 1 300); do
  LANDING_SYNC_ID="$(curl -sS "$TESSERA_API_BASE/uploads/$UPLOAD_ID" "${AUTH[@]}" \
    | python3 -c 'import json,sys; print(json.load(sys.stdin)["sync_id"])')"
  LANDING="$(curl -sS "$TESSERA_API_BASE/syncs/$LANDING_SYNC_ID" "${AUTH[@]}" \
    | python3 -c 'import json,sys; print(json.load(sys.stdin)["status"])')"
  if [ "$LANDING" = "succeeded" ]; then break; fi
  if [ "$LANDING" = "failed" ]; then
    # The read that saw `failed` may itself have handed the file on: re-read
    # the upload once, and keep polling if it now names another sync.
    OWNER="$(curl -sS "$TESSERA_API_BASE/uploads/$UPLOAD_ID" "${AUTH[@]}" \
      | python3 -c 'import json,sys; print(json.load(sys.stdin)["sync_id"])')"
    if [ "$OWNER" = "$LANDING_SYNC_ID" ]; then break; fi
  fi
  sleep 1
done
echo "landing sync: $LANDING"

# --- 3. map columns to canonical events --------------------------------------
curl -sS -X PUT "$TESSERA_API_BASE/sources/$SOURCE_ID/mapping" "${AUTH[@]}" \
  -H "Content-Type: application/json" \
  --data-binary @mapping-orders.json > /dev/null
echo "mapping stored"

# --- 4. re-sync: normalize every row into canonical events -------------------
SYNC="$(curl -sS -X POST "$TESSERA_API_BASE/sources/$SOURCE_ID/syncs" "${AUTH[@]}" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: quickstart-orders-full-sync" \
  -d '{"mode": "full"}')"
SYNC_ID="$(printf '%s' "$SYNC" | python3 -c 'import json,sys; print(json.load(sys.stdin)["sync_id"])')"
for _ in $(seq 1 300); do
  SYNC="$(curl -sS "$TESSERA_API_BASE/syncs/$SYNC_ID" "${AUTH[@]}")"
  STATUS="$(printf '%s' "$SYNC" | python3 -c 'import json,sys; print(json.load(sys.stdin)["status"])')"
  if [ "$STATUS" = "succeeded" ] || [ "$STATUS" = "failed" ]; then break; fi
  sleep 1
done

printf '%s' "$SYNC" | python3 -c '
import json, sys
sync = json.load(sys.stdin)
keys = ("status", "rows_landed", "rows_rejected", "events_normalized")
print(json.dumps({key: sync[key] for key in keys}, indent=2))
'
printf '%s' "$SOURCE_ID" > "$TESSERA_STATE_DIR/source_id"
