<?php
/**
 * PHP script to create sample certification data
 * Run this on the staging server to test the new certification system
 */

// This script should be run from the WordPress root directory
// php test-create-sample-certifications.php

// Load WordPress
if (!defined('ABSPATH')) {
    require_once(dirname(__FILE__) . '/wp-config.php');
}

if (!defined('ABSPATH')) {
    die("Could not load WordPress. Make sure this script is in the WordPress root directory.\n");
}

echo "🚀 Testing HVAC Certification System...\n";
echo "=====================================\n\n";

// Test 1: Check if our classes are loaded
echo "📋 1. Checking if certification classes are loaded...\n";

$classes_to_check = [
    'HVAC_Trainer_Certification_Manager',
    'HVAC_Certification_Migrator', 
    'HVAC_Certification_Admin',
    'HVAC_Trainer_Profile_Manager'
];

$loaded_classes = [];
$missing_classes = [];

foreach ($classes_to_check as $class_name) {
    if (class_exists($class_name)) {
        $loaded_classes[] = $class_name;
        echo "   ✅ {$class_name}: Loaded\n";
    } else {
        $missing_classes[] = $class_name;
        echo "   ❌ {$class_name}: NOT LOADED\n";
    }
}

echo "\n";

// Test 2: Check post type registration
echo "📋 2. Checking post type registration...\n";
$post_types = get_post_types(['public' => true], 'objects');
if (isset($post_types['trainer_certification'])) {
    echo "   ✅ trainer_certification post type: Registered\n";
    echo "      Label: " . $post_types['trainer_certification']->label . "\n";
} else {
    echo "   ❌ trainer_certification post type: NOT REGISTERED\n";
}

echo "\n";

// Test 3: Find a test trainer
echo "📋 3. Finding test trainer...\n";
$trainers = get_users([
    'role' => 'hvac_trainer',
    'number' => 5,
    'fields' => ['ID', 'user_login', 'user_email']
]);

if (empty($trainers)) {
    echo "   ❌ No HVAC trainers found. Looking for any users...\n";
    $trainers = get_users(['number' => 5, 'fields' => ['ID', 'user_login', 'user_email']]);
}

if (empty($trainers)) {
    die("   💥 No users found in the system. Cannot proceed with test.\n");
}

$test_trainer = $trainers[0];
echo "   ✅ Using test trainer: {$test_trainer->user_login} (ID: {$test_trainer->ID})\n";
echo "\n";

// Test 4: Create sample certifications (only if classes are loaded)
if (in_array('HVAC_Trainer_Certification_Manager', $loaded_classes)) {
    echo "📋 4. Creating sample certifications...\n";
    
    $cert_manager = HVAC_Trainer_Certification_Manager::instance();
    
    $sample_certifications = [
        [
            'type' => 'measureQuick Certified Trainer',
            'status' => 'active',
            'issue_date' => '2024-01-15',
            'expiration_date' => '2026-01-15',
            'certification_number' => 'MQT-TEST-001',
            'notes' => 'Test certification - Active and valid'
        ],
        [
            'type' => 'measureQuick Certified Champion',
            'status' => 'active',
            'issue_date' => '2024-06-01',
            'expiration_date' => '2025-01-15', // Expiring soon
            'certification_number' => 'MQC-TEST-001', 
            'notes' => 'Test certification - Expiring soon'
        ],
        [
            'type' => 'measureQuick Certified Trainer',
            'status' => 'expired',
            'issue_date' => '2022-03-10',
            'expiration_date' => '2024-03-10', // Already expired
            'certification_number' => 'MQT-TEST-EXPIRED',
            'notes' => 'Test certification - Expired'
        ]
    ];
    
    $created_certifications = [];
    
    foreach ($sample_certifications as $i => $cert_data) {
        echo "   📝 Creating certification " . ($i + 1) . ": {$cert_data['type']} ({$cert_data['status']})...\n";
        
        try {
            $result = $cert_manager->create_certification(
                $test_trainer->ID,
                $cert_data['type'],
                [
                    'status' => $cert_data['status'],
                    'issue_date' => $cert_data['issue_date'],
                    'expiration_date' => $cert_data['expiration_date'],
                    'certification_number' => $cert_data['certification_number'],
                    'notes' => $cert_data['notes'],
                    'issued_by' => get_current_user_id() ?: 1
                ]
            );
            
            if (is_wp_error($result)) {
                echo "      ❌ Failed: " . $result->get_error_message() . "\n";
            } else {
                echo "      ✅ Created post ID: {$result}\n";
                $created_certifications[] = $result;
                
                // Mark as test data for cleanup
                update_post_meta($result, '_test_certification', true);
            }
            
        } catch (Exception $e) {
            echo "      💥 Exception: " . $e->getMessage() . "\n";
        }
    }
    
    echo "\n";
    
    // Test 5: Verify created data
    if (!empty($created_certifications)) {
        echo "📋 5. Verifying created certifications...\n";
        
        $trainer_certs = $cert_manager->get_trainer_certifications($test_trainer->ID);
        echo "   📊 Found " . count($trainer_certs) . " certifications for trainer {$test_trainer->user_login}\n";
        
        foreach ($trainer_certs as $cert) {
            $cert_type = get_post_meta($cert->ID, 'certification_type', true);
            $status = get_post_meta($cert->ID, 'status', true);
            $exp_date = get_post_meta($cert->ID, 'expiration_date', true);
            
            echo "      🏆 {$cert->post_title}\n";
            echo "         Type: {$cert_type}\n";
            echo "         Status: {$status}\n";
            echo "         Expires: {$exp_date}\n";
        }
        
        echo "\n";
        
        // Test 6: Test the profile manager display method
        if (in_array('HVAC_Trainer_Profile_Manager', $loaded_classes)) {
            echo "📋 6. Testing profile manager display method...\n";
            
            $profile_manager = HVAC_Trainer_Profile_Manager::get_instance();
            
            if (method_exists($profile_manager, 'get_trainer_certifications')) {
                $formatted_certs = $profile_manager->get_trainer_certifications($test_trainer->ID);
                echo "   📊 Profile manager returned " . count($formatted_certs) . " formatted certifications\n";
                
                foreach ($formatted_certs as $cert) {
                    echo "      🎴 {$cert['title']}\n";
                    echo "         Type: {$cert['type']}\n";
                    echo "         Status: {$cert['status']}\n";
                    echo "         Expiration Status: {$cert['expiration_status']}\n";
                    echo "         Display Color: {$cert['display_color']}\n";
                    if (isset($cert['days_until_expiration'])) {
                        echo "         Days until expiration: {$cert['days_until_expiration']}\n";
                    }
                    echo "\n";
                }
            } else {
                echo "   ❌ get_trainer_certifications method not found in profile manager\n";
            }
            
            echo "\n";
        }
    }
    
} else {
    echo "📋 4. SKIPPED: Creating sample certifications (certification manager not loaded)\n\n";
}

// Summary
echo "📋 Test Summary:\n";
echo "===============\n";
echo "✅ Classes loaded: " . count($loaded_classes) . "/" . count($classes_to_check) . "\n";
echo "✅ Test trainer found: {$test_trainer->user_login}\n";

if (isset($created_certifications)) {
    echo "✅ Certifications created: " . count($created_certifications) . "\n";
}

if (!empty($missing_classes)) {
    echo "\n⚠️ Missing classes that need attention:\n";
    foreach ($missing_classes as $class) {
        echo "   - {$class}\n";
    }
}

echo "\n🎉 Test completed!\n";

// Cleanup instructions
if (isset($created_certifications) && !empty($created_certifications)) {
    echo "\n🧹 To cleanup test data, run:\n";
    echo "DELETE FROM wp_posts WHERE post_type = 'trainer_certification' AND ID IN (" . implode(',', $created_certifications) . ");\n";
    echo "DELETE FROM wp_postmeta WHERE post_id IN (" . implode(',', $created_certifications) . ");\n";
}

?>