#!/bin/bash

# Verify certificate page is accessible and contains expected data

echo "=== Testing Certificate Reports Page ==="
echo "This script tests access to the certificate reports page and verifies content"
echo "========================================================"
echo

# Create a cookie file
COOKIE_FILE=$(mktemp)

# Step 1: Login and get authentication cookie
echo "Step 1: Logging in and getting authentication cookie..."
curl -s -c "$COOKIE_FILE" -d "log=test_trainer&pwd=Test123!&wp-submit=Log+In" \
  -X POST "https://wordpress-974670-5399585.cloudwaysapps.com/wp-login.php" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  > /dev/null

# Step 2: Access the dashboard page to verify login succeeded
echo "Step 2: Verifying login by accessing dashboard..."
DASHBOARD_RESPONSE=$(curl -s -b "$COOKIE_FILE" "https://wordpress-974670-5399585.cloudwaysapps.com/hvac-dashboard/")

if [[ $DASHBOARD_RESPONSE == *"HVAC Dashboard"* ]]; then
  echo "✅ Login successful - Dashboard accessible"
else
  echo "❌ Login failed - Could not access dashboard"
  rm "$COOKIE_FILE"
  exit 1
fi

# Step 3: Test certificate reports page
echo "Step 3: Testing certificate reports page..."
CERT_RESPONSE=$(curl -s -b "$COOKIE_FILE" "https://wordpress-974670-5399585.cloudwaysapps.com/certificate-reports/")

if [[ $CERT_RESPONSE == *"Certificate Reports"* ]]; then
  echo "✅ Certificate reports page accessible"
  
  # Check for certificate table
  if [[ $CERT_RESPONSE == *"hvac-certificate-table"* ]]; then
    echo "✅ Certificate table found on page"
    
    # Extract and display total count if available
    if [[ $CERT_RESPONSE =~ Showing[[:space:]]+[0-9]+-[0-9]+[[:space:]]+of[[:space:]]+([0-9]+)[[:space:]]+certificates ]]; then
      TOTAL_COUNT="${BASH_REMATCH[1]}"
      echo "✅ Found $TOTAL_COUNT total certificates"
    else
      echo "❓ Could not determine total certificate count"
    fi
    
    # Check for key event names
    for EVENT_NAME in "HVAC System Design Fundamentals" "Advanced Refrigeration Technology" "Building Automation Systems Workshop"; do
      if [[ $CERT_RESPONSE == *"$EVENT_NAME"* ]]; then
        echo "✅ Found certificates for event: $EVENT_NAME"
      else
        echo "⚠️ No certificates found for event: $EVENT_NAME"
      fi
    done
    
    # Check for Ben Tester
    if [[ $CERT_RESPONSE == *"Ben Tester"* ]]; then
      echo "✅ Found certificates for attendee: Ben Tester"
    else
      echo "⚠️ No certificates found for attendee: Ben Tester"
    fi
  else
    echo "❌ No certificate table found on page"
  fi
else
  echo "❌ Certificate reports page not accessible"
fi

# Step 4: Test event filtering
echo "Step 4: Testing event filtering..."
for EVENT_ID in 5641 5668 5688; do
  FILTER_RESPONSE=$(curl -s -b "$COOKIE_FILE" "https://wordpress-974670-5399585.cloudwaysapps.com/certificate-reports/?filter_event=$EVENT_ID")
  
  if [[ $FILTER_RESPONSE == *"hvac-certificate-table"* ]]; then
    if [[ $FILTER_RESPONSE =~ Showing[[:space:]]+[0-9]+-[0-9]+[[:space:]]+of[[:space:]]+([0-9]+)[[:space:]]+certificates ]]; then
      FILTERED_COUNT="${BASH_REMATCH[1]}"
      echo "✅ Event filter $EVENT_ID returned $FILTERED_COUNT certificates"
    else
      echo "⚠️ Event filter $EVENT_ID returned unknown number of certificates"
    fi
  else
    echo "❌ Event filter $EVENT_ID failed"
  fi
done

# Step 5: Test attendee search
echo "Step 5: Testing attendee search..."
for SEARCH_TERM in "Ben+Tester" "ben%40tealmaker.com" "Smith" "%40gmail"; do
  SEARCH_RESPONSE=$(curl -s -b "$COOKIE_FILE" "https://wordpress-974670-5399585.cloudwaysapps.com/certificate-reports/?search_attendee=$SEARCH_TERM")
  
  DECODED_TERM=$(echo "$SEARCH_TERM" | sed 's/+/ /g' | sed 's/%40/@/g')
  
  if [[ $SEARCH_RESPONSE == *"hvac-certificate-table"* ]]; then
    if [[ $SEARCH_RESPONSE =~ Showing[[:space:]]+[0-9]+-[0-9]+[[:space:]]+of[[:space:]]+([0-9]+)[[:space:]]+certificates ]]; then
      SEARCH_COUNT="${BASH_REMATCH[1]}"
      echo "✅ Attendee search for '$DECODED_TERM' returned $SEARCH_COUNT certificates"
    else
      echo "⚠️ Attendee search for '$DECODED_TERM' returned unknown number of certificates"
    fi
  else
    echo "❌ Attendee search for '$DECODED_TERM' failed"
  fi
done

# Clean up
rm "$COOKIE_FILE"

echo
echo "Certificate test data verification completed."