#!/bin/bash

# Check certificate report URLs with different filtering parameters

echo "=== Testing Certificate Report URLs with Various Filters ==="
echo "This script tests different URL combinations to verify the filters work"
echo "========================================================"
echo

# Base URL
BASE_URL="https://wordpress-974670-5399585.cloudwaysapps.com/certificate-reports/"

# Test cases
declare -a URLS=(
    "$BASE_URL"
    "$BASE_URL?filter_event=5641"
    "$BASE_URL?search_attendee=Ben+Tester"
    "$BASE_URL?search_attendee=ben%40tealmaker.com"
    "$BASE_URL?filter_revoked=1"
    "$BASE_URL?filter_event=5641&search_attendee=Ben+Tester"
)

for url in "${URLS[@]}"; do
    echo "Testing URL: $url"
    
    # Use curl to fetch the URL and check the response
    HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" "$url")
    
    if [ "$HTTP_CODE" -eq 200 ]; then
        echo "  ✅ URL accessible (HTTP $HTTP_CODE)"
        
        # Get the page content and check for certificate table
        CONTENT=$(curl -s "$url")
        
        if [[ $CONTENT == *"hvac-certificate-table"* ]]; then
            echo "  ✅ Certificate table found on page"
            
            # Check if we have any certificates listed
            if [[ $CONTENT == *"No certificates found"* ]]; then
                echo "  ❌ No certificates found with these filters"
            else
                # Try to extract the total count from the content
                TOTAL_COUNT=$(echo "$CONTENT" | grep -o 'Showing [0-9]\+-[0-9]\+ of [0-9]\+ certificates' | grep -o 'of [0-9]\+ certificates' | grep -o '[0-9]\+')
                
                if [ -n "$TOTAL_COUNT" ]; then
                    echo "  ✅ Found $TOTAL_COUNT certificates with these filters"
                else
                    echo "  ⚠️  Could not determine certificate count"
                fi
            fi
        else
            echo "  ❌ Certificate table not found on page"
        fi
    else
        echo "  ❌ URL returned HTTP $HTTP_CODE"
    fi
    
    echo
done

echo "URL tests completed. You should manually verify these URLs in a browser"
echo "to confirm that the filters are working correctly."