<?php

/**
 * Stripe Debug Helper
 *
 * This file provides debugging functions to test the Stripe library loading
 * and help identify any remaining conflicts.
 */

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

/**
 * Debug function to test Stripe library loading
 */
function wpgv_debug_stripe_loading()
{
    error_log('WPGV: ========================================');
    error_log('WPGV: Starting Stripe library debug...');
    error_log('WPGV: ========================================');

    // Test 1: Check if our force loader is available
    if (!function_exists('wpgv_force_load_stripe_library')) {
        error_log('WPGV: FAILED - Force loader function not found');
        return false;
    }
    error_log('WPGV: PASSED - Force loader function found');

    // Test 2: Force load the library
    if (!wpgv_force_load_stripe_library()) {
        error_log('WPGV: FAILED - Could not force load Stripe library');
        return false;
    }
    error_log('WPGV: PASSED - Stripe library force loaded successfully');

    // Test 3: 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 4: Check if Stripe class exists
    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');

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

    // Test 7: List all Stripe classes to see what's available
    $classes = get_declared_classes();
    $stripe_classes = array();
    foreach ($classes as $class) {
        if (strpos($class, 'Stripe\\') === 0) {
            $stripe_classes[] = $class;
        }
    }
    error_log('WPGV: Found ' . count($stripe_classes) . ' Stripe classes: ' . implode(', ', $stripe_classes));

    error_log('WPGV: ========================================');
    error_log('WPGV: All Stripe library tests passed!');
    error_log('WPGV: ========================================');

    return true;
}

/**
 * Test Stripe initialization with a dummy API key
 */
function wpgv_debug_stripe_init()
{
    error_log('WPGV: Testing Stripe initialization...');

    // Use a dummy API key for testing
    $test_key = 'sk_test_1234567890abcdef';

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

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

/**
 * Test creating a Stripe session (will fail with dummy key but we can test the process)
 */
function wpgv_debug_stripe_session()
{
    error_log('WPGV: Testing Stripe session creation...');

    $test_session_data = array(
        'payment_method_types' => ['card'],
        'line_items' => [[
            'price_data' => [
                'currency' => 'usd',
                'unit_amount' => 2000,
                'product_data' => [
                    'name' => 'Test Product',
                ],
            ],
            'quantity' => 1,
        ]],
        'mode' => 'payment',
        'success_url' => 'https://example.com/success',
        'cancel_url' => 'https://example.com/cancel',
    );

    $result = wpgv_create_force_stripe_session($test_session_data);

    if ($result['success']) {
        error_log('WPGV: PASSED - Stripe session creation successful (unexpected with dummy key)');
        return true;
    } else {
        // This is expected to fail with a dummy key, but we can check the error type
        if (
            strpos($result['data'], 'authentication') !== false ||
            strpos($result['data'], 'Invalid API key') !== false
        ) {
            error_log('WPGV: PASSED - Stripe session creation failed as expected with dummy key: ' . $result['data']);
            return true;
        } else {
            error_log('WPGV: FAILED - Stripe session creation failed with unexpected error: ' . $result['data']);
            return false;
        }
    }
}

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

    $test1 = wpgv_debug_stripe_loading();
    $test2 = wpgv_debug_stripe_init();
    $test3 = wpgv_debug_stripe_session();

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

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