#!/usr/bin/env bash
set -euo pipefail

HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# Works both:
# - in-repo:   finish_ready/scripts/validate_all.sh
# - extracted: scripts/validate_all.sh
if [[ -d "$HERE/../engine" && -d "$HERE/../paper" ]]; then
  ROOT_DIR="$(cd "$HERE/.." && pwd)"
elif [[ -d "$HERE/../../engine" && -d "$HERE/../paper" ]]; then
  ROOT_DIR="$(cd "$HERE/../.." && pwd)"
else
  echo "Could not locate repository root (engine/ not found)." 1>&2
  exit 2
fi

ART="$ROOT_DIR/engine/artifacts"
AUD="$ROOT_DIR/engine/audit"
IDX="$ROOT_DIR/engine/contracts/witness_atlas_index.json"

# Audit every shipped artifact so the bundle is self-consistent.

for f in "$ART"/WSC-*.json; do
  python3 "$AUD/audit_wsc.py" "$f"
done

for f in "$ART"/WOB-*.json; do
  python3 "$AUD/audit_wob.py" "$f"
done

for f in "$ART"/CC-*.json; do
  python3 "$AUD/audit_cc.py" "$f"
done

python3 "$AUD/audit_atlas.py" "$IDX" "$ART"

echo "OK: validated all WSC/WOB/CC artifacts and atlas index"

# BENCHMARK sanity (runs inference tools on synthetic data)
python3 "$ROOT_DIR/engine/benchmarks/run_benchmarks.py"
