#!/usr/bin/env bash
# Re-initialise all 6 IACE projects (4 Grenzen-only machines + 2 GTs) with the
# new domain-gated engine, then check for the specific foreign-domain leak
# signatures we set out to eliminate. Run AFTER the ai-sdk container is rebuilt.
# (bash 3.2 compatible — no associative arrays.)
set -uo pipefail

API="https://macmini:8093/sdk/v1/iace"
TEN="9282a473-5c95-4b3a-bf78-0ecc0ec71d3e"
PSQL='/usr/local/bin/docker exec bp-core-postgres psql -U breakpilot -d breakpilot_db -At -F"|"'

PROJECTS="
Kniehebelpresse:1e00ca16-ed0f-456d-80dd-b3035e413370
Eigenbauzelle:1f17051e-5ed1-4f56-a734-1bbcdc5c0c48
Elektromotoren:79566165-1561-43c7-97bf-4b6421a0641d
Schwingarm:00a573ec-caa0-4b7e-8af6-0acf3ed62989
Kistenhub_GT:1646d728-a6cb-4275-b147-44ca282bb6f0
Bremse_GT:f9347149-3d03-4c12-8a8e-0f8e32a71a91
"

echo "########## RE-INIT (force=true) — Summary vs DB ##########"
for row in $PROJECTS; do
  name="${row%%:*}"; pid="${row##*:}"
  resp=$(curl -sk -X POST -H "X-Tenant-ID: $TEN" "$API/projects/$pid/initialize?force=true")
  sum=$(echo "$resp" | python3 -c "import sys,json
try:
  s=json.load(sys.stdin).get('summary',{})
  print(f\"comp={s.get('components')} pat={s.get('patterns')} haz={s.get('hazards')} mit={s.get('mitigations')} norm={s.get('norms')}\")
except Exception as e: print('(keine summary)')" 2>/dev/null)
  hz=$(ssh macmini "$PSQL -c \"SELECT count(*) FROM compliance.iace_hazards WHERE project_id='$pid';\"" 2>/dev/null)
  # Konsistenz: summary.haz muss == DB-Hazards sein (Summary-Bug-Check)
  shz=$(echo "$sum" | sed -nE 's/.*haz=([0-9]+).*/\1/p')
  ok="OK"; [ "$shz" = "$hz" ] || ok="!! MISMATCH"
  printf "  %-16s summary[%s]  DB-Hazards=%s  -> %s\n" "$name" "$sum" "$hz" "$ok"
done

echo
echo "########## LEAK-SIGNATUR-CHECK (Grenzen-Maschinen, sollte 0 sein) ##########"
LEAKS="Schwimmbecken|Massageduesen|Nassbereich|Karussell|Fahrersitz|Lackier|Loesemitteldampf|Tanklager|Reaktor|Sauerstoffanreicherung|Spannfutterbacke|Bandsaege|Folienwickler|Blasformwerkzeug|Asbest|Roboterzelle|Schwenkbereich Roboter|Spaeneflug|Spanflug"
for row in $PROJECTS; do
  name="${row%%:*}"; pid="${row##*:}"
  case "$name" in *_GT) continue;; esac
  echo "=== $name ==="
  ssh macmini "$PSQL -c \"SELECT category||' | '||name FROM compliance.iace_hazards WHERE project_id='$pid' AND (name ~* '($LEAKS)' OR hazardous_zone ~* '($LEAKS)') ORDER BY name;\"" 2>/dev/null \
    | sed 's/^/    LEAK: /'
  cnt=$(ssh macmini "$PSQL -c \"SELECT count(*) FROM compliance.iace_hazards WHERE project_id='$pid' AND (name ~* '($LEAKS)' OR hazardous_zone ~* '($LEAKS)');\"" 2>/dev/null)
  echo "    -> Leak-Treffer: $cnt"
done
