<?php
/**
 * Plugin Status Checker
 * 
 * Checks the status of required plugins for the HVAC Community Events system
 */

// Load WordPress
require_once('wp-load.php');

echo "===== PLUGIN STATUS CHECK =====\n\n";

// Check for required plugins
$required_plugins = [
    'The Events Calendar' => [
        'class' => 'Tribe__Events__Main',
        'constant' => 'TRIBE_EVENTS_FILE',
        'file' => 'the-events-calendar/the-events-calendar.php'
    ],
    'Events Calendar Pro' => [
        'class' => 'Tribe__Events__Pro__Main',
        'constant' => 'EVENTS_CALENDAR_PRO_FILE',
        'file' => 'events-pro/events-calendar-pro.php'
    ],
    'Event Tickets' => [
        'class' => 'Tribe__Tickets__Main',
        'constant' => 'EVENT_TICKETS_MAIN_PLUGIN_FILE',
        'file' => 'event-tickets/event-tickets.php'
    ],
    'Event Tickets Plus' => [
        'class' => 'Tribe__Tickets_Plus__Main',
        'constant' => 'EVENT_TICKETS_PLUS_FILE',
        'file' => 'event-tickets-plus/event-tickets-plus.php'
    ],
    'The Events Calendar: Community Events' => [
        'class' => 'Tribe__Events__Community__Main',
        'constant' => 'EVENTS_COMMUNITY_FILE',
        'file' => 'the-events-calendar-community-events/tribe-community-events.php'
    ],
    'HVAC Community Events' => [
        'class' => 'HVAC_Community_Events',
        'constant' => 'HVAC_CE_PLUGIN_FILE',
        'file' => 'hvac-community-events/hvac-community-events.php'
    ]
];

foreach ($required_plugins as $plugin_name => $plugin_data) {
    echo "Checking $plugin_name:\n";
    
    // Check if class exists
    $class_exists = class_exists($plugin_data['class']);
    echo " - Class {$plugin_data['class']} exists: " . ($class_exists ? "Yes ✅" : "No ❌") . "\n";
    
    // Check if constant is defined
    $constant_defined = defined($plugin_data['constant']);
    echo " - Constant {$plugin_data['constant']} defined: " . ($constant_defined ? "Yes ✅" : "No ❌") . "\n";
    
    // Check if plugin is active using WordPress function
    $is_active = is_plugin_active($plugin_data['file']);
    echo " - Plugin is active: " . ($is_active ? "Yes ✅" : "No ❌") . "\n";
    
    echo "\n";
}

// Check specific providers for Event Tickets
if (class_exists('Tribe__Tickets_Plus__Commerce__PayPal__Main')) {
    echo "Event Tickets Plus - PayPal Provider:\n";
    $paypal = Tribe__Tickets_Plus__Commerce__PayPal__Main::get_instance();
    echo " - Instance created: " . ($paypal ? "Yes ✅" : "No ❌") . "\n";
    echo " - Provider is active: " . (method_exists($paypal, 'is_active') && $paypal->is_active() ? "Yes ✅" : "No ❌") . "\n";
    echo "\n";
}

// Check HVAC Certificate Manager
if (class_exists('HVAC_Certificate_Manager')) {
    echo "HVAC Certificate Manager:\n";
    $cert_manager = HVAC_Certificate_Manager::instance();
    echo " - Instance created: " . ($cert_manager ? "Yes ✅" : "No ❌") . "\n";
    
    // Check certificate table
    global $wpdb;
    $table_name = $wpdb->prefix . 'hvac_certificates';
    $table_exists = $wpdb->get_var("SHOW TABLES LIKE '$table_name'") === $table_name;
    echo " - Certificate table exists: " . ($table_exists ? "Yes ✅" : "No ❌") . "\n";
    
    if ($table_exists) {
        $cert_count = $wpdb->get_var("SELECT COUNT(*) FROM $table_name");
        echo " - Certificate count: $cert_count\n";
    }
    
    echo "\n";
}

// Show active plugins
echo "All Active Plugins:\n";
$active_plugins = get_option('active_plugins');
if (is_array($active_plugins)) {
    foreach ($active_plugins as $plugin) {
        echo " - $plugin\n";
    }
} else {
    echo " - No active plugins found\n";
}

echo "\n===== PLUGIN CHECK COMPLETE =====\n";