#!/bin/bash
# =====================================================================
# run-accessibility-tests.sh - Run all accessibility tests
# =====================================================================
# This script runs all accessibility tests for WCAG 2.1 AAA compliance
# =====================================================================

set -e

# Check if base URL is provided
if [ -z "$1" ]; then
  BASE_URL="http://localhost:8080"
else
  BASE_URL="$1"
fi

TESTS_DIR="$(dirname "$0")"
REPORTS_DIR="$TESTS_DIR/../reports"
mkdir -p "$REPORTS_DIR"

echo "=== Running Accessibility Tests ==="
echo "Using base URL: $BASE_URL"

# Track test results
FAILED_TESTS=0

# Run Pa11y tests
echo "Running Pa11y tests..."
if command -v npx &> /dev/null && npx pa11y --version &> /dev/null; then
  if "$TESTS_DIR/pa11y-test.sh" "$BASE_URL"; then
    echo "✅ Pa11y tests passed"
  else
    echo "❌ Pa11y tests failed"
    FAILED_TESTS=$((FAILED_TESTS + 1))
  fi
else
  echo "❌ Pa11y not installed, skipping Pa11y tests"
  echo "   Install with: npm install -g pa11y"
  FAILED_TESTS=$((FAILED_TESTS + 1))
fi

# Run axe-core tests with Playwright
echo "Running axe-core tests with Playwright..."
if command -v node &> /dev/null; then
  if [ -f "$TESTS_DIR/playwright-axe.js" ]; then
    if node "$TESTS_DIR/playwright-axe.js" "$BASE_URL"; then
      echo "✅ axe-core tests passed"
    else
      echo "❌ axe-core tests failed"
      FAILED_TESTS=$((FAILED_TESTS + 1))
    fi
  else
    echo "❌ playwright-axe.js not found"
    FAILED_TESTS=$((FAILED_TESTS + 1))
  fi
else
  echo "❌ Node.js not installed, skipping axe-core tests"
  FAILED_TESTS=$((FAILED_TESTS + 1))
fi

# Remind about manual testing
echo "Don't forget to complete the manual testing checklist:"
echo "$TESTS_DIR/manual-checklist.md"

# Generate combined report
echo "Generating combined accessibility report..."
TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
cat > "$REPORTS_DIR/accessibility-summary.json" << EOJSON
{
  "timestamp": "$TIMESTAMP",
  "baseUrl": "$BASE_URL",
  "summary": {
    "automated": {
      "pa11y": {
        "status": "$([ -f "$REPORTS_DIR/pa11y-summary.json" ] && echo "completed" || echo "failed")",
        "report": "pa11y-summary.json"
      },
      "axe": {
        "status": "$([ -f "$REPORTS_DIR/axe-summary.json" ] && echo "completed" || echo "failed")",
        "report": "axe-summary.json"
      }
    },
    "manual": {
      "status": "pending",
      "checklist": "../accessibility/manual-checklist.md"
    }
  }
}
EOJSON

# Print summary
echo "=== Accessibility Test Summary ==="
echo "Tests completed at: $TIMESTAMP"
echo "Reports saved to: $REPORTS_DIR"

if [ "$FAILED_TESTS" -gt 0 ]; then
  echo "❌ $FAILED_TESTS test suites failed"
  exit 1
else
  echo "✅ All automated test suites passed"
  echo "⚠️ Manual testing still required - see checklist"
  exit 0
fi
