source "$(dirname "${BASH_SOURCE[0]}")/env-init.sh"
#!/usr/bin/env bash
set -euo pipefail

# Guided smoke checks for assets/app.js behavior.
# Usage:
#   ./tools/app_js_smoke_check.sh [base_url]
# Example:
#   ./tools/app_js_smoke_check.sh http://127.0.0.1:8090

BASE_URL="${1:-http://127.0.0.1:8090}"
ROOT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
PASS_COUNT=0
WARN_COUNT=0
FAIL_COUNT=0
TMP_DIR="$(mktemp -d)"

cleanup() {
  rm -rf "$TMP_DIR"
}
trap cleanup EXIT

pass() {
  printf "[PASS] %s\n" "$1"
  PASS_COUNT=$((PASS_COUNT + 1))
}

warn() {
  printf "[WARN] %s\n" "$1"
  WARN_COUNT=$((WARN_COUNT + 1))
}

fail() {
  printf "[FAIL] %s\n" "$1"
  FAIL_COUNT=$((FAIL_COUNT + 1))
}

section() {
  printf "\n== %s ==\n" "$1"
}

require_file() {
  local f="$1"
  if [[ -f "$ROOT_DIR/$f" ]]; then
    pass "Found $f"
  else
    fail "Missing required file: $f"
  fi
}

check_html_hook() {
  local hook="$1"
  if grep -q "$hook" "$ROOT_DIR/templ.php"; then
    pass "templ.php includes $hook"
  else
    fail "templ.php missing $hook"
  fi
}

ask_yes_no() {
  local prompt="$1"
  local answer=""
  while true; do
    read -r -p "$prompt [y/n]: " answer
    answer="$(printf "%s" "$answer" | tr '[:upper:]' '[:lower:]')"
    case "$answer" in
      y|yes) return 0 ;;
      n|no) return 1 ;;
      *) printf "Please answer y or n.\n" ;;
    esac
  done
}

section "Workspace checks"
require_file "assets/app.js"
require_file "assets/app.js.README.md"
require_file "templ.php"
require_file "header.php"
require_file "footer.php"

section "Static checks"
if command -v node >/dev/null 2>&1; then
  if node --check "$ROOT_DIR/assets/app.js" >/dev/null 2>&1; then
    pass "JavaScript syntax check passed (node --check assets/app.js)"
  else
    fail "JavaScript syntax check failed for assets/app.js"
  fi
else
  warn "Node not found; skipped JS syntax check"
fi

check_html_hook "id=\"content-area\""
check_html_hook "id=\"page-loader\""
check_html_hook "id=\"page-loader-bar\""
check_html_hook "id=\"spinner-overlay\""
check_html_hook "id=\"toast-container\""
check_html_hook "id=\"sidebar-backdrop\""

section "HTTP smoke checks"
MAIN_HTML="$TMP_DIR/main.html"
AJAX_HTML="$TMP_DIR/ajax_mainContent.html"

if curl -fsS "$BASE_URL/templ.php?page=mainContent" >"$MAIN_HTML"; then
  pass "GET $BASE_URL/templ.php?page=mainContent"
else
  fail "Could not fetch main page. Is your PHP server running at $BASE_URL?"
fi

if [[ -s "$MAIN_HTML" ]]; then
  if grep -q "assets/app.js" "$MAIN_HTML"; then
    pass "Main page references assets/app.js"
  else
    fail "Main page did not reference assets/app.js"
  fi
fi

if curl -fsS "$BASE_URL/templ.php?page=mainContent&label=Dashboard&ajax=1" >"$AJAX_HTML"; then
  pass "AJAX fragment endpoint responded"
else
  fail "AJAX fragment request failed"
fi

if [[ -s "$AJAX_HTML" ]]; then
  if grep -qi "fatal error\|parse error\|warning" "$AJAX_HTML"; then
    warn "AJAX fragment output contains possible PHP warning/error text"
  else
    pass "AJAX fragment output has no obvious PHP error text"
  fi
fi

section "Guided manual checks"
printf "Open this URL in your browser: %s/templ.php?page=mainContent\n" "$BASE_URL"
printf "Then follow prompts below.\n\n"

if ask_yes_no "Sidebar links load content without full page refresh"; then
  pass "Sidebar AJAX navigation"
else
  fail "Sidebar AJAX navigation"
fi

if ask_yes_no "Browser back/forward keeps content and breadcrumb in sync"; then
  pass "History + breadcrumb behavior"
else
  fail "History + breadcrumb behavior"
fi

if ask_yes_no "Theme selector (auto/light/dark) updates UI mode"; then
  pass "Theme mode switching"
else
  fail "Theme mode switching"
fi

if ask_yes_no "Console toast command works: window.App.toast.show('Smoke', 'info', 1200)"; then
  pass "Toast API"
else
  fail "Toast API"
fi

if ask_yes_no "Console spinner command works: window.App.spinner.show('Test'); setTimeout(() => window.App.spinner.hide(), 700);"; then
  pass "Spinner API"
else
  fail "Spinner API"
fi

if ask_yes_no "Unsaved user settings prompt appears before navigation away from Settings"; then
  pass "Unsaved changes guard"
else
  warn "Unsaved changes guard not verified/passed"
fi

section "Summary"
printf "Passed: %d\n" "$PASS_COUNT"
printf "Warnings: %d\n" "$WARN_COUNT"
printf "Failed: %d\n" "$FAIL_COUNT"

if [[ "$FAIL_COUNT" -gt 0 ]]; then
  printf "\nSmoke check FAILED.\n"
  exit 1
fi

printf "\nSmoke check PASSED (warnings may still need review).\n"
