#!/bin/bash
# ====================================================
# PR-B Architecture Guard — Master Runner
# OpenClaw Agent OS v2 — Architecture Stabilization
# ====================================================
# Runs all 6 architecture guards.
# Exit 0 = all guards pass
# Exit 1 = one or more guards failed
# ====================================================

set -e

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
ROOT_DIR="$(dirname "$SCRIPT_DIR")"
cd "$ROOT_DIR"

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
NC='\033[0m'

PASS_COUNT=0
FAIL_COUNT=0
WARN_COUNT=0
TOTAL=6

# Store results in temp file
RESULT_FILE="$(mktemp)"
trap "rm -f $RESULT_FILE" EXIT

echo ""
echo -e "${CYAN}══════════════════════════════════════════════${NC}"
echo -e "${CYAN}  OpenClaw Agent OS v2 — Architecture Guard${NC}"
echo -e "${CYAN}  PR-B: $(date '+%Y-%m-%d %H:%M:%S %Z')${NC}"
echo -e "${CYAN}══════════════════════════════════════════════${NC}"

run_guard() {
  local name="$1"
  local script="$2"
  local mode="$3"  # "hard" or "soft"
  
  echo ""
  echo -e "${YELLOW}──────────────────────────────────────────${NC}"
  
  if node "$script" 2>&1; then
    PASS_COUNT=$((PASS_COUNT + 1))
    echo "$name|PASS" >> "$RESULT_FILE"
    echo -e "${GREEN}  ✓ $name passed${NC}"
    return 0
  else
    local exit_code=$?
    if [ "$mode" = "hard" ]; then
      FAIL_COUNT=$((FAIL_COUNT + 1))
      echo "$name|FAIL" >> "$RESULT_FILE"
      echo -e "${RED}  ✗ $name FAILED (exit $exit_code)${NC}"
      return 1
    else
      WARN_COUNT=$((WARN_COUNT + 1))
      echo "$name|WARN" >> "$RESULT_FILE"
      echo -e "${YELLOW}  ⚠ $name warning (exit $exit_code)${NC}"
      return 0
    fi
  fi
}

# Run all 6 guards
run_guard "Guard-1: Memory Backend"   "scripts/guard-memory-backend.mjs"  "hard"
run_guard "Guard-2: Runtime Core"     "scripts/guard-runtime-core.mjs"    "hard"
run_guard "Guard-3: Tool Path"        "scripts/guard-tool-path.mjs"       "soft"
run_guard "Guard-4: MEMORY.md Write"  "scripts/guard-memory-write.mjs"    "hard"
run_guard "Guard-5: Tool Trace"       "scripts/guard-tool-trace.mjs"      "soft"
run_guard "Guard-6: Dreaming Phase"   "scripts/guard-dreaming-phase.mjs"  "hard"

# Summary
echo ""
echo -e "${CYAN}══════════════════════════════════════════════${NC}"
echo -e "${CYAN}  Architecture Guard Summary${NC}"
echo -e "${CYAN}══════════════════════════════════════════════${NC}"
echo ""

while IFS='|' read -r name result; do
  case "$result" in
    PASS) echo -e "  ${GREEN}[PASS]${NC} $name" ;;
    FAIL) echo -e "  ${RED}[FAIL]${NC} $name" ;;
    WARN) echo -e "  ${YELLOW}[WARN]${NC} $name" ;;
  esac
done < "$RESULT_FILE"

echo ""
echo -e "  Total: $TOTAL | ${GREEN}Pass: $PASS_COUNT${NC} | ${RED}Fail: $FAIL_COUNT${NC} | ${YELLOW}Warn: $WARN_COUNT${NC}"

if [ $FAIL_COUNT -eq 0 ]; then
  echo ""
  echo -e "${GREEN}  ✓ ALL ARCHITECTURE GUARDS PASSED${NC}"
  echo -e "${GREEN}  System is within allowed architecture boundaries.${NC}"
  exit 0
else
  echo ""
  echo -e "${RED}  ✗ ARCHITECTURE VIOLATIONS DETECTED${NC}"
  echo -e "${RED}  Fix failures before proceeding.${NC}"
  exit 1
fi
