#!/usr/bin/env bash
# Regression test for cf-schema-check.sh - the canonical acceptance-schema
# validator. Input on stdin is the DDL text a `SELECT sql FROM sqlite_master`
# returns; exit 0 iff the store matches the one canonical `acceptances` shape,
# non-zero on any drift. No network, no D1.
#
# Covers:
#   1. canonical shape -> exit 0
#   2. canonical shape + an unrelated leads table -> exit 0 (extra tables fine)
#   3. bare `name` instead of signer_name -> exit 1, stderr names signer drift
#   4. missing swept column -> exit 1, stderr names swept
#   5. a stray dual_acceptances table -> exit 1, stderr names it
#   6. no acceptances table (only leads) -> exit 1, stderr "no acceptances table"
set -u

HELPER="$(cd "$(dirname "$0")/.." && pwd)/cf-schema-check.sh"
[ -x "$HELPER" ] || { echo "FAIL: $HELPER not executable" >&2; exit 1; }

PASS=0; FAIL=0
ok()  { echo "PASS: $1"; PASS=$((PASS+1)); }
bad() { echo "FAIL: $1" >&2; FAIL=$((FAIL+1)); }

# Canonical acceptance DDL (the authority declared in d1-data-capture.md).
CANON='CREATE TABLE IF NOT EXISTS acceptances (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  doc_ref TEXT NOT NULL,
  doc_type TEXT NOT NULL,
  party TEXT,
  signer_name TEXT,
  accepted_at TEXT NOT NULL DEFAULT (datetime('"'"'now'"'"')),
  swept INTEGER NOT NULL DEFAULT 0
);'

LEADS='CREATE TABLE leads (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  email TEXT NOT NULL,
  swept INTEGER NOT NULL DEFAULT 0
);'

# Run helper with stdin=$1. Sets ERR/RC.
run() {
  local ef; ef=$(mktemp)
  printf '%s\n' "$1" | bash "$HELPER" 2>"$ef" >/dev/null
  RC=$?
  ERR=$(cat "$ef"); rm -f "$ef"
}

# 1. canonical -> 0
run "$CANON"
[ "$RC" -eq 0 ] && ok "canonical shape accepted (exit=$RC)" || bad "canonical rejected (exit=$RC err='$ERR')"

# 2. canonical + leads -> 0
run "$CANON
$LEADS"
[ "$RC" -eq 0 ] && ok "canonical plus unrelated table accepted" || bad "extra table rejected (exit=$RC err='$ERR')"

# 3. bare name drift -> 1, mentions signer
DRIFT_NAME='CREATE TABLE acceptances (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  doc_ref TEXT NOT NULL,
  doc_type TEXT NOT NULL,
  party TEXT,
  name TEXT,
  accepted_at TEXT NOT NULL,
  swept INTEGER NOT NULL DEFAULT 0
);'
run "$DRIFT_NAME"
{ [ "$RC" -ne 0 ] && printf '%s' "$ERR" | grep -qi "signer"; } \
  && ok "bare name rejected, signer drift named (exit=$RC)" \
  || bad "bare name drift (exit=$RC err='$ERR')"

# 4. missing swept -> 1, mentions swept
DRIFT_SWEPT='CREATE TABLE acceptances (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  doc_ref TEXT NOT NULL,
  doc_type TEXT NOT NULL,
  party TEXT,
  signer_name TEXT,
  accepted_at TEXT NOT NULL
);'
run "$DRIFT_SWEPT"
{ [ "$RC" -ne 0 ] && printf '%s' "$ERR" | grep -qi "swept"; } \
  && ok "missing swept rejected, swept named (exit=$RC)" \
  || bad "missing swept (exit=$RC err='$ERR')"

# 4b. acceptances missing swept, but a sibling leads table HAS swept -> still
#     exit 1. Column checks must be scoped to the acceptances table, not bleed
#     across the whole dump.
DRIFT_SWEPT_BLEED='CREATE TABLE acceptances (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  doc_ref TEXT NOT NULL,
  doc_type TEXT NOT NULL,
  party TEXT,
  signer_name TEXT,
  accepted_at TEXT NOT NULL
);
CREATE TABLE leads (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  email TEXT NOT NULL,
  swept INTEGER NOT NULL DEFAULT 0
);'
run "$DRIFT_SWEPT_BLEED"
{ [ "$RC" -ne 0 ] && printf '%s' "$ERR" | grep -qi "swept"; } \
  && ok "missing swept not masked by sibling table (exit=$RC)" \
  || bad "swept bleed across tables (exit=$RC err='$ERR')"

# 5. dual_acceptances stray -> 1, mentions it
DUAL="$CANON
CREATE TABLE dual_acceptances (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  doc_ref TEXT NOT NULL,
  party TEXT,
  signer_name TEXT
);"
run "$DUAL"
{ [ "$RC" -ne 0 ] && printf '%s' "$ERR" | grep -qi "dual_acceptances"; } \
  && ok "stray dual_acceptances rejected and named (exit=$RC)" \
  || bad "dual_acceptances stray (exit=$RC err='$ERR')"

# 6. no acceptances table -> 1, mentions it
run "$LEADS"
{ [ "$RC" -ne 0 ] && printf '%s' "$ERR" | grep -qi "no acceptances"; } \
  && ok "missing acceptances table rejected and named (exit=$RC)" \
  || bad "no acceptances table (exit=$RC err='$ERR')"

echo "----"
echo "PASS=$PASS FAIL=$FAIL"
[ "$FAIL" -eq 0 ]
