<?php
/**
 * Test Enhanced CSV Import on Staging
 */

require_once '../../../wp-load.php';
require_once 'includes/enhanced-csv-import-from-file.php';

echo "=== Enhanced CSV Import Test on Staging ===\n\n";

// Check if CSV file exists
$csv_file = "CSV_Trainers_Import_1Aug2025.csv";
echo "CSV File: $csv_file\n";
echo "CSV Exists: " . (file_exists($csv_file) ? "✅ Yes" : "❌ No") . "\n\n";

if (!file_exists($csv_file)) {
    echo "❌ CSV file not found in plugin directory\n";
    exit(1);
}

// Check taxonomies
echo "=== Checking Taxonomies ===\n";
$taxonomies = ["business_type", "training_audience"];
foreach ($taxonomies as $taxonomy) {
    $exists = taxonomy_exists($taxonomy);
    echo "$taxonomy: " . ($exists ? "✅ Available" : "❌ Missing") . "\n";
}

// Count existing profiles before import
$existing_profiles = wp_count_posts("trainer_profile")->publish ?? 0;
echo "\nExisting profiles: $existing_profiles\n\n";

echo "=== Running Enhanced Import ===\n";
$start_time = microtime(true);

try {
    $results = execute_enhanced_csv_import();
    $end_time = microtime(true);
    $execution_time = round($end_time - $start_time, 2);
    
    echo "\n=== Import Results ===\n";
    echo "Execution time: {$execution_time} seconds\n";
    echo "Total rows: " . ($results["total_rows"] ?? 0) . "\n";
    echo "Users created: " . ($results["users_created"] ?? 0) . "\n";
    echo "Users updated: " . ($results["users_updated"] ?? 0) . "\n";
    echo "Profiles created: " . ($results["profiles_created"] ?? 0) . "\n";
    echo "Profiles updated: " . ($results["profiles_updated"] ?? 0) . "\n";
    echo "Taxonomies assigned: " . ($results["taxonomies_assigned"] ?? 0) . "\n";
    echo "Venues created: " . ($results["venues_created"] ?? 0) . "\n";
    echo "Organizers created: " . ($results["organizers_created"] ?? 0) . "\n";
    echo "Errors: " . ($results["errors"] ?? 0) . "\n";
    
    if (isset($results['fatal_error'])) {
        echo "\n❌ Fatal Error: " . $results['fatal_error'] . "\n";
    }
    
    // Verification
    $new_profile_count = wp_count_posts("trainer_profile")->publish ?? 0;
    echo "\nTotal profiles after import: $new_profile_count\n";
    
    // Check first few imported profiles with their taxonomy data
    $profiles = get_posts([
        "post_type" => "trainer_profile",
        "posts_per_page" => 5,
        "orderby" => "date",
        "order" => "DESC"
    ]);
    
    echo "\n=== Sample Imported Profiles ===\n";
    foreach ($profiles as $profile) {
        echo "- " . $profile->post_title . " (ID: " . $profile->ID . ")\n";
        
        // Check profile meta
        $email = get_post_meta($profile->ID, 'trainer_email', true);
        $phone = get_post_meta($profile->ID, 'trainer_phone', true);
        $company = get_post_meta($profile->ID, 'company_name', true);
        
        echo "  Email: $email\n";
        echo "  Phone: $phone\n";
        echo "  Company: $company\n";
        
        // Check taxonomy assignments
        $business_terms = get_the_terms($profile->ID, "business_type");
        if ($business_terms && !is_wp_error($business_terms)) {
            $business_names = wp_list_pluck($business_terms, "name");
            echo "  Business Type: " . implode(", ", $business_names) . "\n";
        }
        
        $audience_terms = get_the_terms($profile->ID, "training_audience");
        if ($audience_terms && !is_wp_error($audience_terms)) {
            $audience_names = wp_list_pluck($audience_terms, "name");
            echo "  Training Audience: " . implode(", ", $audience_names) . "\n";
        }
        
        echo "\n";
    }
    
    echo "✅ Enhanced CSV import completed successfully!\n";
    
} catch (Exception $e) {
    echo "❌ Import failed: " . $e->getMessage() . "\n";
    echo "Stack trace:\n" . $e->getTraceAsString() . "\n";
}
?>