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

BASE_REF="${BASE_REF:-origin/main}"
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
cd "$ROOT"

git rev-parse --verify "$BASE_REF" >/dev/null

ALLOWLIST=(
  "docs/architecture/PLAN-ADR-025-I3-otel-bridge-2026q2.md"
  "schemas/otel_bridge_report_v1.schema.json"
  "scripts/ci/review-adr025-i3-step1.sh"
)

echo "[review] allowlist + workflow-ban"
git diff --name-only "$BASE_REF"...HEAD | while IFS= read -r f; do
  [[ -z "$f" ]] && continue

  if [[ "$f" == .github/workflows/* ]]; then
    echo "FAIL: I3 Step1 must not change workflows ($f)"
    exit 1
  fi

  ok="false"
  for a in "${ALLOWLIST[@]}"; do
    [[ "$f" == "$a" ]] && ok="true" && break
  done

  if [[ "$ok" != "true" ]]; then
    echo "FAIL: file not allowed in I3 Step1: $f"
    exit 1
  fi
done

echo "[review] schema JSON parses"
python3 - <<'PY'
import json
json.load(open("schemas/otel_bridge_report_v1.schema.json","r",encoding="utf-8"))
print("otel_bridge_report_v1 schema: ok")
PY

echo "[review] done"
