#!/bin/sh
# Copyright (C) 2026 Ben Weis <ben@springbird.app>
#
# See LICENSE in the repository root for license terms.

# Differential check of Stannum against the original Lead reference.
#
# Lead is the deliberately slow, correctness-first implementation this project
# started from. This script clones PlanetScale's Lead (or uses LEAD_REF_DIR if
# it already holds a checkout), builds it under its own extension name, `tin`,
# into the PostgreSQL that pg_config on PATH describes, creates two databases
# on the server named by the usual libpq variables, and runs the oracle: 47
# query shapes across five mutation states. Match sets, rank order, and HTML/ANSI highlights must
# agree; score bits are not compared, because Lead counts token-less documents
# present at index build in its corpus size and scores expansions as zero,
# where Stannum follows TIN.
#
#   PGHOST=localhost PGPORT=28818 script/reference-oracle [output-dir]
#   LEAD_REF=<branch-or-commit> pins a fresh reference checkout (default: main).
#   An existing LEAD_REF_DIR is used as-is; the actual revision is recorded.
#   DATASET=<verified corpus> TRACE=<queries.json> enables the read-only
#   published-trace oracle instead: exact membership/full_score bits/top-10,
#   with ROWS=5000 and BUDGET_SECONDS=900 defaults. Build/provisioning time is
#   outside that verification budget. The result is written under OUT/trace.
#   LIFECYCLE=1 enables the separate four-row SQL lifecycle suite.
#   BOUNDARIES=1 enables the separate 100-row documented contract suite.
set -eu
if [ "${LIFECYCLE:-0}" = 1 ] && { [ "${BOUNDARIES:-0}" = 1 ] || [ -n "${DATASET:-}" ]; }; then
  echo 'LIFECYCLE cannot be combined with BOUNDARIES or DATASET' >&2
  exit 2
fi
if [ "${BOUNDARIES:-0}" = 1 ] && [ -n "${DATASET:-}" ]; then
  echo 'BOUNDARIES and DATASET are mutually exclusive' >&2
  exit 2
fi
LEAD_REPOSITORY="${LEAD_REPOSITORY:-https://github.com/planetscale/lead.git}"
LEAD_REF="${LEAD_REF:-main}"
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
OUT="${1:-$ROOT/benchmarks/results/reference-oracle-$(date +%Y%m%d-%H%M%S)}"
REF="${LEAD_REF_DIR:-${TMPDIR:-/tmp}/stannum-lead-ref}"
PG_FEATURE="pg$(pg_config --version | sed -E 's/PostgreSQL ([0-9]+).*/\1/')"

if [ ! -d "$REF/.git" ] && [ ! -f "$REF/.git" ]; then
  git clone --quiet "$LEAD_REPOSITORY" "$REF"
  git -C "$REF" checkout --quiet "$LEAD_REF"
fi
if [ -n "${DATASET:-}" ]; then
  : "${TRACE:?TRACE must name the published queries.json}"
fi
echo "building Lead $(git -C "$REF" rev-parse --short HEAD) as extension 'tin' for $PG_FEATURE"
( cd "$REF/postgres" && CARGO_TARGET_DIR="${LEAD_TARGET_DIR:-$REF/target}" \
    cargo pgrx install --release --no-default-features --features "$PG_FEATURE" >/dev/null )
echo "installing Stannum for $PG_FEATURE"
( cd "$ROOT/postgres" && cargo pgrx install --release --no-default-features --features "$PG_FEATURE" >/dev/null )

for db in reference_oracle_stannum reference_oracle_lead; do
  dropdb --if-exists "$db"
  createdb "$db"
done
# Record actual callable signatures: absent/changed functions fail observation,
# never silently disable highlight comparison.
mkdir -p "$OUT"
psql -X -v ON_ERROR_STOP=1 -d reference_oracle_lead -c 'CREATE EXTENSION tin' >/dev/null
psql -X -v ON_ERROR_STOP=1 -d reference_oracle_lead -c '\df tin.*highlight*' > "$OUT/lead-highlight-functions.txt"
git -C "$REF" rev-parse HEAD > "$OUT/lead-revision.txt"
LEFT="$(mktemp)"; RIGHT="$(mktemp)"
printf 'PGDATABASE=reference_oracle_stannum\n' > "$LEFT"
printf 'PGDATABASE=reference_oracle_lead\n' > "$RIGHT"
status=0
if [ "${LIFECYCLE:-0}" = 1 ]; then
  python3 "$ROOT/benchmarks/oracle.py" --left "$LEFT" --right "$RIGHT" \
    --lifecycle --reference-source "$REF" --budget-seconds "${BUDGET_SECONDS:-120}" \
    --output "$OUT/lifecycle" || status=$?
elif [ "${BOUNDARIES:-0}" = 1 ]; then
  python3 "$ROOT/benchmarks/oracle.py" --left "$LEFT" --right "$RIGHT" \
    --boundaries --reference-source "$REF" --budget-seconds "${BUDGET_SECONDS:-120}" \
    --output "$OUT/boundaries" || status=$?
elif [ -n "${DATASET:-}" ]; then
  python3 "$ROOT/benchmarks/oracle.py" --left "$LEFT" --left-engine stannum \
    --right "$RIGHT" --right-engine tin --scores bits --rows "${ROWS:-5000}" \
    --dataset "$DATASET" --trace "${TRACE:?TRACE must name the published queries.json}" \
    --reference-source "$REF" --budget-seconds "${BUDGET_SECONDS:-900}" \
    --output "$OUT/trace" || status=$?
else
python3 "$ROOT/benchmarks/oracle.py" --left "$LEFT" --left-engine stannum \
  --right "$RIGHT" --right-engine tin --scores order --rows "${ROWS:-5000}" \
  --output "$OUT" || status=$?
fi
rm -f "$LEFT" "$RIGHT"
dropdb --if-exists reference_oracle_stannum
dropdb --if-exists reference_oracle_lead
exit $status
