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

# Reject only identifier families whose use of "surface" or "canonical" was
# reviewed and found to hide a more precise domain meaning. This deliberately
# does not ban either English word: genuine UI surfaces, canonicalization, and
# frozen client/wire/storage contracts remain valid.
#
# Usage:
#   bash guardrails/naming/semantic-terminology.sh [--check]
#
# Default mode reports matches. --check fails when a retired family returns.

repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
cd "$repo_root"

denylist="guardrails/naming/retired-ambiguous-identifiers.txt"
exceptions="guardrails/naming/semantic-terminology-exceptions.txt"
check=0
[[ "${1:-}" == "--check" ]] && check=1

python3 guardrails/naming/scan-identifiers.py --self-test >/dev/null

set +e
python3 guardrails/naming/scan-identifiers.py \
  --root "$repo_root" \
  --rules "$denylist" \
  --exceptions "$exceptions"
status=$?
set -e

if [[ $status -eq 2 ]]; then
  exit 2
fi

if [[ $status -eq 1 ]]; then
  echo "== semantic terminology guardrail: retired identifier hits remain =="
else
  echo "== semantic terminology guardrail: no retired identifier hits =="
fi
if [[ $check -eq 1 && $status -eq 1 ]]; then
  echo "FAIL: a retired ambiguous identifier family was reintroduced" >&2
  exit 1
fi
