<?php

/**
 * Stripe Library Test
 *
 * This file tests the Stripe library isolation to ensure it works correctly
 * and prevents conflicts with other plugins.
 */

if (!defined('ABSPATH')) exit;  // Exit if accessed directly

/**
 * Test Stripe library functionality
 */
function wpgv_test_stripe_library()
{
    error_log('WPGV: Starting Stripe library test...');

    // Test 1: Check if our isolated library loads
    if (!wpgv_load_isolated_stripe()) {
        error_log('WPGV: FAILED - Could not load isolated Stripe library');
        return false;
    }
    error_log('WPGV: PASSED - Isolated Stripe library loaded successfully');

    // Test 2: Check if Checkout Session class exists
    if (!class_exists('\Stripe\Checkout\Session')) {
        error_log('WPGV: FAILED - Checkout Session class not found');
        return false;
    }
    error_log('WPGV: PASSED - Checkout Session class found');

    // Test 3: Try to create a test session object
    try {
        $test_session = new \Stripe\Checkout\Session();
        error_log('WPGV: PASSED - Successfully created Checkout Session object');
    } catch (Exception $e) {
        error_log('WPGV: FAILED - Could not create Checkout Session object: ' . $e->getMessage());
        return false;
    }

    // Test 4: Check if Stripe class exists and has required methods
    if (!class_exists('\Stripe\Stripe')) {
        error_log('WPGV: FAILED - Stripe class not found');
        return false;
    }
    error_log('WPGV: PASSED - Stripe class found');

    // Test 5: Check if setApiKey method exists
    if (!method_exists('\Stripe\Stripe', 'setApiKey')) {
        error_log('WPGV: FAILED - setApiKey method not found');
        return false;
    }
    error_log('WPGV: PASSED - setApiKey method found');

    error_log('WPGV: All Stripe library tests passed!');
    return true;
}

/**
 * Test Stripe initialization
 */
function wpgv_test_stripe_initialization()
{
    error_log('WPGV: Testing Stripe initialization...');

    // Use a test API key (this will fail but we can test the initialization process)
    $test_key = 'sk_test_1234567890abcdef';

    if (!wpgv_init_isolated_stripe($test_key)) {
        error_log('WPGV: FAILED - Stripe initialization failed');
        return false;
    }

    error_log('WPGV: PASSED - Stripe initialization successful');
    return true;
}

/**
 * Run all tests
 */
function wpgv_run_stripe_tests()
{
    error_log('WPGV: ========================================');
    error_log('WPGV: Starting comprehensive Stripe tests...');
    error_log('WPGV: ========================================');

    $test1 = wpgv_test_stripe_library();
    $test2 = wpgv_test_stripe_initialization();

    if ($test1 && $test2) {
        error_log('WPGV: ========================================');
        error_log('WPGV: ALL TESTS PASSED! Stripe isolation is working.');
        error_log('WPGV: ========================================');
        return true;
    } else {
        error_log('WPGV: ========================================');
        error_log('WPGV: SOME TESTS FAILED! Stripe isolation needs attention.');
        error_log('WPGV: ========================================');
        return false;
    }
}

// Run tests if this file is accessed directly (for debugging)
if (isset($_GET['wpgv_test_stripe']) && current_user_can('manage_options')) {
    wpgv_run_stripe_tests();
}
