<?php
/**
 * ExploreXR License Configuration Loader
 * 
 * This file loads the license configuration constants if they are not already defined.
 * It serves as a fallback configuration for development and testing.
 */

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

// Load API configuration constants if not already defined (e.g., in wp-config.php)

if (!function_exists('explorexr_premium_get_license_env')) {
    /**
     * Read license configuration from Docker-friendly environment variables.
     *
     * Supports both VAR and VAR_FILE, matching the official WordPress Docker image pattern.
     */
    function explorexr_premium_get_license_env($name, $default = '') {
        $file_env = getenv($name . '_FILE');
        if ($file_env && is_readable($file_env)) {
            return rtrim((string) file_get_contents($file_env), "\r\n");
        }

        $value = getenv($name);
        if ($value !== false) {
            return (string) $value;
        }

        return $default;
    }
}

// WooCommerce API Consumer Key
// SECURITY: Define EXPLOREXR_WC_CONSUMER_KEY in wp-config.php
// Example: define('EXPLOREXR_WC_CONSUMER_KEY', 'ck_your_key_here');
if (!defined('EXPLOREXR_WC_CONSUMER_KEY')) {
    define('EXPLOREXR_WC_CONSUMER_KEY', explorexr_premium_get_license_env('EXPLOREXR_WC_CONSUMER_KEY', ''));
}

// WooCommerce API Consumer Secret
// SECURITY: Define EXPLOREXR_WC_CONSUMER_SECRET in wp-config.php
// Example: define('EXPLOREXR_WC_CONSUMER_SECRET', 'cs_your_secret_here');
if (!defined('EXPLOREXR_WC_CONSUMER_SECRET')) {
    define('EXPLOREXR_WC_CONSUMER_SECRET', explorexr_premium_get_license_env('EXPLOREXR_WC_CONSUMER_SECRET', ''));
}

// License Manager API Consumer Key (uses WooCommerce key if not separately defined)
if (!defined('EXPLOREXR_LM_CONSUMER_KEY')) {
    define('EXPLOREXR_LM_CONSUMER_KEY', explorexr_premium_get_license_env('EXPLOREXR_LM_CONSUMER_KEY', EXPLOREXR_WC_CONSUMER_KEY));
}

// License Manager API Consumer Secret (uses WooCommerce secret if not separately defined)
if (!defined('EXPLOREXR_LM_CONSUMER_SECRET')) {
    define('EXPLOREXR_LM_CONSUMER_SECRET', explorexr_premium_get_license_env('EXPLOREXR_LM_CONSUMER_SECRET', EXPLOREXR_WC_CONSUMER_SECRET));
}

// ExploreXR public license service endpoint.
// This endpoint should proxy License Manager calls server-side so customer sites do not need API secrets.
if (!defined('EXPLOREXR_LICENSE_SERVER_URL')) {
    define('EXPLOREXR_LICENSE_SERVER_URL', explorexr_premium_get_license_env('EXPLOREXR_LICENSE_SERVER_URL', 'https://expoxr.com/wp-json/explorexr/v1/license/'));
}

// Product IDs for different license tiers (optional, for advanced validation)
if (!defined('EXPLOREXR_PRO_PRODUCT_ID')) {
    define('EXPLOREXR_PRO_PRODUCT_ID', 682); // Replace with actual product ID
}

if (!defined('EXPLOREXR_PLUS_PRODUCT_ID')) {
    define('EXPLOREXR_PLUS_PRODUCT_ID', 731); // Replace with actual product ID
}

if (!defined('EXPLOREXR_ULTRA_PRODUCT_ID')) {
    define('EXPLOREXR_ULTRA_PRODUCT_ID', 734); // Replace with actual product ID
}

// Debug flag for development
if (!defined('EXPLOREXR_LICENSE_DEBUG')) {
    define('EXPLOREXR_LICENSE_DEBUG', false); // Set to true only for development debugging
}

// License configuration loaded - debugging available via WP_DEBUG constant
