#!/bin/bash

# Debug Certificate System
# This script helps debug issues with the certificate functionality in the HVAC Community Events plugin.

# Colors
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Function to print colored messages
print_message() {
    local type=$1
    local message=$2
    local color=$BLUE
    
    case $type in
        "info")
            color=$BLUE
            ;;
        "success")
            color=$GREEN
            ;;
        "warning")
            color=$YELLOW
            ;;
        "error")
            color=$RED
            ;;
    esac
    
    echo -e "${color}${message}${NC}"
}

# Check if we have a WordPress installation
if [ ! -d "wordpress" ]; then
    print_message "error" "No WordPress installation found in ./wordpress directory."
    print_message "info" "Please run this script from the wordpress-dev directory."
    exit 1
fi

# Check that we're in the right directory
if [ ! -f "bin/debug-certificate-system.sh" ]; then
    print_message "error" "Please run this script from the wordpress-dev directory."
    exit 1
fi

print_message "info" "=== HVAC Certificate System Debug ==="
print_message "info" ""

# Step 1: Check plugin files
print_message "info" "Step 1: Checking plugin files..."
if [ -d "wordpress/wp-content/plugins/hvac-community-events" ]; then
    print_message "success" "✓ Plugin directory found."
else
    print_message "error" "✗ Plugin directory not found."
    exit 1
fi

# Check critical files
critical_files=(
    "wordpress/wp-content/plugins/hvac-community-events/hvac-community-events.php"
    "wordpress/wp-content/plugins/hvac-community-events/includes/certificates/class-certificate-manager.php"
    "wordpress/wp-content/plugins/hvac-community-events/includes/certificates/class-certificate-installer.php"
    "wordpress/wp-content/plugins/hvac-community-events/templates/certificates/template-certificate-reports.php"
    "wordpress/wp-content/plugins/hvac-community-events/templates/certificates/template-generate-certificates.php"
)

missing_files=0
for file in "${critical_files[@]}"; do
    if [ -f "$file" ]; then
        print_message "success" "✓ Found: $file"
    else
        print_message "error" "✗ Missing: $file"
        missing_files=$((missing_files+1))
    fi
done

if [ $missing_files -gt 0 ]; then
    print_message "warning" "! $missing_files critical files are missing."
else
    print_message "success" "All critical files are present."
fi

# Step 2: Check and fix database tables
print_message "info" ""
print_message "info" "Step 2: Checking database tables..."
php bin/check-and-fix-certificate-tables.php

# Step 3: Debug certificate reports
print_message "info" ""
print_message "info" "Step 3: Debugging certificate reports functionality..."
php bin/debug-certificate-reports.php

# Final summary
print_message "info" ""
print_message "info" "=== Debug Summary ==="
print_message "info" "1. Check the output above for errors or warnings."
print_message "info" "2. If there are database issues, try deactivating and reactivating the plugin."
print_message "info" "3. Check PHP error logs for detailed error messages."
print_message "info" "4. If issues persist, the problem might be in the frontend JavaScript or CSS."
print_message "info" ""
print_message "success" "Debug process completed."

chmod +x bin/check-and-fix-certificate-tables.php
chmod +x bin/debug-certificate-reports.php