#!/bin/bash
# =====================================================================
# pa11y-test.sh - Run Pa11y accessibility tests
# =====================================================================
# This script runs Pa11y 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 "=== Testing WCAG 2.1 AAA Compliance with Pa11y ==="
echo "Using base URL: $BASE_URL"

# Get URLs from sitemap
SITEMAP_URL="$BASE_URL/sitemap.xml"
echo "Fetching pages from sitemap: $SITEMAP_URL"
URLS=($(curl -s "$SITEMAP_URL" | grep -o '<loc>.*</loc>' | sed 's/<loc>//;s/<\/loc>//'))

if [ ${#URLS[@]} -eq 0 ]; then
    echo "No URLs found in sitemap, or sitemap could not be fetched."
    exit 1
fi

echo "Found ${#URLS[@]} pages to test."

# Track failures
FAILURES=0

# Run tests for each URL
for url in "${URLS[@]}"; do
  echo "Testing ${url}..."
  
  # Create a report file name from the URL
  report_file_name="pa11y-$(echo "$url" | sed -e 's|https://.*\.com/||' -e 's|/|-|g' -e 's/\.html//').json"
  report_file="$REPORTS_DIR/$report_file_name"
  
  # Run Pa11y and save results
  if npx pa11y --standard WCAG2AAA --reporter json --ignore "content-security-policy" "${url}" > "${report_file}" 2>/dev/null; then
    issues=$(jq '.issues | length' "${report_file}" 2>/dev/null || echo 0)
    echo "Found ${issues} issues on ${url}"
    
    # Check if there are issues
    if [ "${issues}" -gt 0 ]; then
      FAILURES=$((FAILURES + 1))
      
      # Print summary of issues
      echo "Issues summary:"
      jq -r '.issues[] | "- \(.type): \(.message)\n  Selector: \(.selector)"' "${report_file}" 2>/dev/null | head -n 10
      
      # If there are more than 5 issues, indicate that
      if [ "${issues}" -gt 5 ]; then
        echo "... and $((issues - 5)) more issues."
      fi
    fi
  else
    echo "Error running Pa11y on ${url}"
    # Check if a report file was created with error info
    if [ -f "${report_file}" ]; then
        error_message=$(jq -r '.error' "${report_file}" 2>/dev/null)
        if [ -n "$error_message" ]; then
            echo "  Pa11y error: $error_message"
        fi
    fi
    FAILURES=$((FAILURES + 1))
  fi
  
  echo "---"
done

# Create summary report
jq -n --arg timestamp "$(date -u +"%Y-%m-%dT%H:%M:%SZ")" \
     --arg baseUrl "${BASE_URL}" \
     --arg failures "${FAILURES}" \
     --arg total "${#URLS[@]}" \
     '{
       "timestamp": $timestamp,
       "baseUrl": $baseUrl,
       "summary": {
         "total": ($total | tonumber),
         "failures": ($failures | tonumber),
         "passed": (($total | tonumber) - ($failures | tonumber))
       }
     }' > "${REPORTS_DIR}/pa11y-summary.json"

# Exit with appropriate status
if [ "${FAILURES}" -gt 0 ]; then
  exit 1
else
  exit 0
fi
