<?php
/**
 * Remote Taxonomy Testing Script
 * Test taxonomy implementation on staging server
 */

// Define WordPress bootstrap
define('SHORTINIT', true);
require_once '../../../wp-load.php';

// Include the taxonomy migration if it exists
$migration_file = ABSPATH . 'wp-content/plugins/hvac-community-events/includes/taxonomy-migration.php';
if (file_exists($migration_file)) {
    require_once $migration_file;
}

class TaxonomyTester {
    private $tests_passed = 0;
    private $tests_failed = 0;
    
    public function run_test($test_name, $test_function, $expected = null) {
        echo "\n🔍 Testing: $test_name\n";
        
        try {
            $result = call_user_func($test_function);
            
            if ($expected === null || $result === $expected || (is_string($expected) && strpos($result, $expected) !== false)) {
                echo "✅ PASS: $test_name\n";
                $this->tests_passed++;
                return true;
            } else {
                echo "❌ FAIL: $test_name - Expected: $expected, Got: $result\n";
                $this->tests_failed++;
                return false;
            }
        } catch (Exception $e) {
            echo "❌ FAIL: $test_name - Error: " . $e->getMessage() . "\n";
            $this->tests_failed++;
            return false;
        }
    }
    
    public function get_results() {
        return [
            'passed' => $this->tests_passed,
            'failed' => $this->tests_failed,
            'total' => $this->tests_passed + $this->tests_failed
        ];
    }
}

$tester = new TaxonomyTester();

echo "🧪 HVAC Taxonomy Implementation Test Suite\n";
echo "==========================================\n";

echo "\n📋 Phase 1: Taxonomy Registration Tests\n";

// Test 1: Check if taxonomies are registered
$tester->run_test('Business Type taxonomy registered', function() {
    return taxonomy_exists('business_type') ? 'exists' : 'missing';
}, 'exists');

$tester->run_test('Training Audience taxonomy registered', function() {
    return taxonomy_exists('training_audience') ? 'exists' : 'missing';
}, 'exists');

$tester->run_test('Training Formats taxonomy registered', function() {
    return taxonomy_exists('training_formats') ? 'exists' : 'missing';
}, 'exists');

$tester->run_test('Training Locations taxonomy registered', function() {
    return taxonomy_exists('training_locations') ? 'exists' : 'missing';
}, 'exists');

$tester->run_test('Training Resources taxonomy registered', function() {
    return taxonomy_exists('training_resources') ? 'exists' : 'missing';
}, 'exists');

echo "\n📋 Phase 2: Default Terms Tests\n";

// Test 2: Check if default terms exist
$tester->run_test('Business Type has default terms', function() {
    $terms = get_terms(['taxonomy' => 'business_type', 'hide_empty' => false]);
    return is_wp_error($terms) ? 0 : count($terms);
}, 7);

$tester->run_test('Training Audience has default terms', function() {
    $terms = get_terms(['taxonomy' => 'training_audience', 'hide_empty' => false]);
    return is_wp_error($terms) ? 0 : count($terms);
}, 4);

$tester->run_test('Training Formats has default terms', function() {
    $terms = get_terms(['taxonomy' => 'training_formats', 'hide_empty' => false]);
    return is_wp_error($terms) ? 0 : count($terms);
}, 4);

$tester->run_test('Training Locations has default terms', function() {
    $terms = get_terms(['taxonomy' => 'training_locations', 'hide_empty' => false]);
    return is_wp_error($terms) ? 0 : count($terms);
}, 5);

$tester->run_test('Training Resources has default terms', function() {
    $terms = get_terms(['taxonomy' => 'training_resources', 'hide_empty' => false]);
    return is_wp_error($terms) ? 0 : count($terms);
}, 12);

echo "\n📋 Phase 3: Post Type Tests\n";

$tester->run_test('Trainer Profile post type registered', function() {
    return post_type_exists('trainer_profile') ? 'exists' : 'missing';
}, 'exists');

$tester->run_test('Trainer profiles exist in database', function() {
    $profiles = get_posts(['post_type' => 'trainer_profile', 'numberposts' => 1]);
    return count($profiles) > 0 ? 'exists' : 'none';
});

echo "\n📋 Phase 4: Class Integration Tests\n";

$tester->run_test('HVAC_Trainer_Profile_Manager class exists', function() {
    return class_exists('HVAC_Trainer_Profile_Manager') ? 'exists' : 'missing';
}, 'exists');

$tester->run_test('HVAC_Geocoding_Ajax class exists', function() {
    return class_exists('HVAC_Geocoding_Ajax') ? 'exists' : 'missing';
}, 'exists');

if (class_exists('HVAC_Taxonomy_Migration')) {
    $tester->run_test('HVAC_Taxonomy_Migration class exists', function() {
        return 'exists';
    }, 'exists');
}

echo "\n📋 Phase 5: Term Validation Tests\n";

// Test specific default terms
$tester->run_test('Business Type contains Manufacturer', function() {
    $term = get_term_by('name', 'Manufacturer', 'business_type');
    return $term ? 'exists' : 'missing';
}, 'exists');

$tester->run_test('Training Audience contains "Anyone (open to the public)"', function() {
    $term = get_term_by('name', 'Anyone (open to the public)', 'training_audience');
    return $term ? 'exists' : 'missing';
}, 'exists');

$tester->run_test('Training Formats contains In-person', function() {
    $term = get_term_by('name', 'In-person', 'training_formats');
    return $term ? 'exists' : 'missing';
}, 'exists');

$tester->run_test('Training Locations contains Online', function() {
    $term = get_term_by('name', 'Online', 'training_locations');
    return $term ? 'exists' : 'missing';
}, 'exists');

$tester->run_test('Training Resources contains Classroom', function() {
    $term = get_term_by('name', 'Classroom', 'training_resources');
    return $term ? 'exists' : 'missing';
}, 'exists');

echo "\n📋 Phase 6: Profile-Taxonomy Integration Tests\n";

// Test with first available trainer profile
$profiles = get_posts(['post_type' => 'trainer_profile', 'numberposts' => 1]);
if (!empty($profiles)) {
    $profile_id = $profiles[0]->ID;
    
    $tester->run_test('Can assign business type to trainer profile', function() use ($profile_id) {
        $result = wp_set_post_terms($profile_id, ['Contractor'], 'business_type');
        return is_wp_error($result) ? 'error' : 'success';
    }, 'success');
    
    $tester->run_test('Business type assignment persists', function() use ($profile_id) {
        $terms = get_the_terms($profile_id, 'business_type');
        return (!empty($terms) && !is_wp_error($terms)) ? $terms[0]->name : 'none';
    }, 'Contractor');
    
    $tester->run_test('Can remove business type from trainer profile', function() use ($profile_id) {
        $result = wp_remove_object_terms($profile_id, ['Contractor'], 'business_type');
        return is_wp_error($result) ? 'error' : 'success';
    }, 'success');
} else {
    echo "⚠️  No trainer profiles found - skipping profile-taxonomy integration tests\n";
}

// Results summary
$results = $tester->get_results();
echo "\n📊 Test Results Summary\n";
echo "=======================\n";
echo "Tests Passed: " . $results['passed'] . "\n";
echo "Tests Failed: " . $results['failed'] . "\n";
echo "Total Tests: " . $results['total'] . "\n";

if ($results['failed'] === 0) {
    echo "\n🎉 All tests passed! Taxonomy implementation is working correctly.\n";
    exit(0);
} else {
    echo "\n⚠️  Some tests failed. Please review the implementation.\n";
    exit(1);
}
?>