#!/bin/bash
# test.all.sh - sh standards: take arguments; -m menu is circular so user can run individual options: test dir test apps test all etc; one test for all + summary what worked/not
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
MENU=0
if [ "$1" == "-m" ] || [ "$1" == "--menu" ]; then MENU=1; fi

run_test_dir(){
  echo "=== Test dir ==="
 ./files.missing.sh; echo "---";./files.perm.sh
}

run_test_apps(){
  echo "=== Test apps (aiwizard) ==="
  echo "Check wizard.json: $(test -f $ROOT/data/wizard.json && echo PASS || echo FAIL)"
  echo "Check build dir: $(test -d $ROOT/data/build && echo PASS || echo FAIL)"
  echo "Check aistd dir: $(test -d $ROOT/docs/aistd && echo PASS || echo FAIL)"
  echo "Count build: $(ls $ROOT/data/build/*.md 2>/dev/null | wc -l) files"
  echo "Count aistd: $(ls $ROOT/docs/aistd/*.md 2>/dev/null | wc -l) files"
}

run_test_all(){
  echo "=== Test all - summary what worked/not ==="
  PASS=0; FAIL=0; RESULTS=""
  test_one(){ if eval "$2"; then echo "PASS: $1"; PASS=$((PASS+1)); RESULTS="$RESULTS\nPASS: $1"; else echo "FAIL: $1"; FAIL=$((FAIL+1)); RESULTS="$RESULTS\nFAIL: $1"; fi; }
  test_one "wizard.json exists" "test -f $ROOT/data/wizard.json"
  test_one "build dir writable" "test -w $ROOT/data/build"
  test_one "aistd dir writable" "test -w $ROOT/docs/aistd"
  test_one "manifest.json exists" "test -f $ROOT/config/docs.manifest.json"
  test_one "form_fields.json exists" "test -f $ROOT/config/form_fields.json"
  test_one "faq.php exists" "test -f $ROOT/pages/help/faq.php"
  test_one "guide.php exists" "test -f $ROOT/pages/help/guide.php"
  test_one "navbar.php exists" "test -f $ROOT/templates/navbar.php"
  test_one "content-start.php exists" "test -f $ROOT/templates/content-start.php"
  test_one "build.php exists" "test -f $ROOT/php/build.php"
  test_one "no hardcoded paths in build.php" "! grep -q '/Users/rbutare/websites/system/php/paths.php' $ROOT/php/build.php || grep -q '\$dir/../../system' $ROOT/php/build.php"
  echo -e "\n=== SUMMARY ==="
  echo -e "$RESULTS"
  echo "PASS: $PASS / $((PASS+FAIL)), FAIL: $FAIL"
  echo "What worked: $PASS tests passed"
  echo "What not: $FAIL tests failed"
  if [ $FAIL -eq 0 ]; then echo "All OK ✓"; exit 0; else echo "$FAIL failures - run audit"; exit 1; fi
}

if [ $MENU -eq 1 ]; then
  while true; do
    echo ""
    echo "=== Circular Menu -m ==="
    echo "1) test dir (files.missing + files.perm)"
    echo "2) test apps (aiwizard specific)"
    echo "3) test all (summary what worked/not)"
    echo "4) audit (run test.all)"
    echo "5) fix perms (files.perm.sh --fix)"
    echo "6) fix owner (sudo chown)"
    echo "7) exit"
    read -p "Choose [1-7]: " choice
    case $choice in
      1) run_test_dir;;
      2) run_test_apps;;
      3) run_test_all;;
      4) run_test_all;;
      5) "$ROOT/tools/files.perm.sh" --fix;;
      6) sudo chown -R $(whoami):staff "$ROOT/" && echo "Owner fixed";;
      7) exit 0;;
      *) echo "Invalid";;
    esac
  done
else
  case "$1" in
    dir) run_test_dir;;
    apps) run_test_apps;;
    all|"") run_test_all;;
    *) echo "Usage: $0 [dir|apps|all] [-m menu circular]"; run_test_all;;
  esac
fi
