<?php

/**
 * Stripe Library Isolation
 *
 * This file provides namespace isolation to prevent conflicts with other plugins
 * that load different versions of the Stripe library.
 */

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

// Define our custom namespace for Stripe
if (!defined('WPGV_STRIPE_NAMESPACE')) {
    define('WPGV_STRIPE_NAMESPACE', 'WPGV_Stripe');
}

/**
 * Load Stripe library with namespace isolation
 */
function wpgv_load_isolated_stripe()
{
    static $loaded = false;

    if ($loaded) {
        return true;
    }

    // Check if we already have a working Stripe library
    if (class_exists('\Stripe\Checkout\Session')) {
        try {
            // Test if the existing library works
            $test = new \Stripe\Checkout\Session();
            $loaded = true;
            return true;
        } catch (Exception $e) {
            // Existing library doesn't work properly, we need to load our own
        }
    }

    // Try to load our Stripe library
    $stripe_paths = array(
        WPGIFT__PLUGIN_DIR . '/vendor/stripe/stripe-php/init.php',
        WPGIFT__PLUGIN_DIR . '/library/stripe-php/init.php'
    );

    foreach ($stripe_paths as $path) {
        if (file_exists($path)) {
            try {
                require_once($path);

                // Verify the library loaded correctly
                if (class_exists('\Stripe\Checkout\Session')) {
                    $loaded = true;
                    error_log('WPGV: Successfully loaded isolated Stripe library from: ' . $path);
                    return true;
                }
            } catch (Exception $e) {
                error_log('WPGV: Failed to load Stripe library from: ' . $path . ' - ' . $e->getMessage());
                continue;
            }
        }
    }

    error_log('WPGV: Could not load any Stripe library');
    return false;
}

/**
 * Initialize Stripe with API key using isolated library
 *
 * @param string $secret_key The Stripe secret key
 * @return bool True if initialization successful, false otherwise
 */
function wpgv_init_isolated_stripe($secret_key)
{
    if (!wpgv_load_isolated_stripe()) {
        return false;
    }

    try {
        \Stripe\Stripe::setApiKey($secret_key);
        \Stripe\Stripe::setAppInfo('GiftVoucherPro', '1.0.0', 'https://giftvoucherpro.com');
        error_log('WPGV: Isolated Stripe initialized successfully');
        return true;
    } catch (Exception $e) {
        error_log('WPGV: Isolated Stripe initialization error: ' . $e->getMessage());
        return false;
    }
}

/**
 * Create Stripe Checkout Session using isolated library
 *
 * @param array $session_data The session data for Stripe Checkout
 * @return array Array with 'success' => bool and 'data' => mixed
 */
function wpgv_create_isolated_stripe_session($session_data)
{
    try {
        if (!wpgv_load_isolated_stripe()) {
            return array(
                'success' => false,
                'data' => 'Stripe library not found'
            );
        }

        error_log('WPGV: Creating isolated Stripe session with data: ' . json_encode($session_data));

        $session = \Stripe\Checkout\Session::create($session_data);

        error_log('WPGV: Isolated Stripe session created successfully: ' . $session->id);

        return array(
            'success' => true,
            'data' => $session
        );
    } catch (\Stripe\Exception\ApiErrorException $e) {
        error_log('WPGV: Isolated Stripe API Error: ' . $e->getMessage());
        return array(
            'success' => false,
            'data' => 'Stripe API error: ' . $e->getMessage()
        );
    } catch (Exception $e) {
        error_log('WPGV: Isolated Stripe General Error: ' . $e->getMessage());
        return array(
            'success' => false,
            'data' => 'Payment processing error: ' . $e->getMessage()
        );
    }
}

// Load the isolated Stripe library early
wpgv_load_isolated_stripe();
