<?php

namespace orderflow;

use orderflow\Admin\Analytics\Incomplete_Orders_Dashboard;
/**
 * Class Init
 *
 * @package EasyElements
 */
class Init {
    use \orderflow\Traits\Core;
    /**
     * Load plugin necessary class.
     */
    public static function setup() {
        // Load PYS_PRO_GLOBAL namespace autoloader FIRST (for prefixed vendor packages)
        if ( file_exists( ADS_PLUGIN_PATH . '/vendor/autoload-pys.php' ) ) {
            require_once ADS_PLUGIN_PATH . '/vendor/autoload-pys.php';
        }
        
        // Load vendor autoload (all packages including Facebook SDK, Guzzle, PSR with PYS_PRO_GLOBAL namespace)
        // Check if required dependencies are already loaded (from pixelyoursite or other plugins)
        if ( file_exists( ADS_PLUGIN_PATH . '/vendor/autoload.php' ) ) {
            self::load_vendor_autoload();
        }
        
        // Load PixelYourSite compatible classes (only for Facebook Pixel)
        require_once ADS_PLUGIN_PATH . '/includes/facebook/class-plugin.php';
        require_once ADS_PLUGIN_PATH . '/includes/facebook/class-settings.php';
        require_once ADS_PLUGIN_PATH . '/includes/facebook/interface-pixel.php';
        require_once ADS_PLUGIN_PATH . '/includes/facebook/class-event-id-generator.php';
        require_once ADS_PLUGIN_PATH . '/includes/facebook/functions-common.php';
        require_once ADS_PLUGIN_PATH . '/includes/facebook/functions-woo.php';
        require_once ADS_PLUGIN_PATH . '/includes/facebook/functions-gdpr.php';
        require_once ADS_PLUGIN_PATH . '/includes/facebook/class-consent.php';
        require_once ADS_PLUGIN_PATH . '/includes/facebook/logger/class-pys-logger.php';
        require_once ADS_PLUGIN_PATH . '/includes/facebook/events/class-event.php';
        require_once ADS_PLUGIN_PATH . '/includes/facebook/events/interface-events.php';
        require_once ADS_PLUGIN_PATH . '/includes/facebook/events/class-event-single.php';
        require_once ADS_PLUGIN_PATH . '/includes/facebook/events/class-event-grouped.php';
        require_once ADS_PLUGIN_PATH . '/includes/facebook/events/class-events-woo.php';
        require_once ADS_PLUGIN_PATH . '/includes/facebook/class-events-manager.php';
        require_once ADS_PLUGIN_PATH . '/includes/facebook/class-events-manager-ajax_hook.php';
		require_once ADS_PLUGIN_PATH . '/includes/facebook/class-purchase-event-manager.php';
		require_once ADS_PLUGIN_PATH . '/includes/facebook/class-custom-order-status-event-manager.php';
		require_once ADS_PLUGIN_PATH . '/includes/facebook/class-custom-events-manager.php';
		require_once ADS_PLUGIN_PATH . '/includes/facebook/class-pys.php';
        
        // Load Facebook module
        require_once ADS_PLUGIN_PATH . '/includes/facebook/modules/facebook/facebook.php';
        require_once ADS_PLUGIN_PATH . '/includes/facebook/modules/facebook/facebook-server.php';
        require_once ADS_PLUGIN_PATH . '/includes/facebook/modules/facebook/class-facebook-rest-api.php';
        
        // Initialize OrderFlow Facebook Pixel (this will register Facebook pixel)
        \orderflow\Facebook\PYS();
        
        // Create Facebook() function alias in orderflow\Facebook namespace after PYS is loaded
        if ( ! function_exists( 'orderflow\Facebook\Facebook' ) ) {
            eval('namespace orderflow\\Facebook { function Facebook() { return \\orderflow\\Facebook\\Facebook::instance(); } }');
        }
        
        // Initialize Facebook REST API
        add_action( 'init', function() {
            \orderflow\Facebook\Facebook_REST_API();
        }, 9 );
        
        if ( is_admin() ) {
            $core = new Core();
            $core::init();
            $incomplete_order = new Admin\incomplete\Incomplete();
            $incomplete_orders_dashboard = new Admin\Incomplete_Orders_Dashboard();
            new Admin\Register_Menus( $incomplete_order,$incomplete_orders_dashboard,$core );

            new Admin\Order_List_Columns();
            new Admin\Customer_Access();
            new Admin\Courier_Details_Meta_Box();
        }
        new Incomplete_Orders_Tracker();
        new Custom_Order_Statuses();

        new Incomplete_Order();
        new Checkout_Restrictions();
        new Assets();
        new Courier_Rate_Partial_Payment();
        new OrderAmountModifier();
    }
    
    /**
     * Safely load vendor autoloader with conflict detection
     * Checks if autoloader class already exists before loading
     */
    private static function load_vendor_autoload() {
        $autoloadPath = ADS_PLUGIN_PATH . '/vendor/autoload.php';
        $autoloadRealPath = ADS_PLUGIN_PATH . '/vendor/composer/autoload_real.php';
        
        if ( ! file_exists( $autoloadPath ) || ! file_exists( $autoloadRealPath ) ) {
            return;
        }
        
        // Extract autoloader class name from autoload_real.php dynamically
        $content = file_get_contents( $autoloadRealPath );
        if ( preg_match( '/class\s+(ComposerAutoloaderInit[a-f0-9]+)/i', $content, $matches ) ) {
            $autoloaderClass = $matches[1];
            
            // Check if this class already exists (loaded by another plugin like pixelyoursite)
            if ( class_exists( $autoloaderClass ) ) {
                // Already loaded by another plugin, use existing instance
                // Check if required classes are available
                if ( ! class_exists( 'Jaybizzle\CrawlerDetect\CrawlerDetect' ) ) {
                    // Even though autoloader is loaded, our classes might not be registered
                    // Try to get the loader and add our paths
                    try {
                        $loader = $autoloaderClass::getLoader();
                        // The loader should already have our classes if dependencies match
                    } catch ( \Exception $e ) {
                        // If getting loader fails, load our own autoloader
                        require_once $autoloadPath;
                    }
                }
                return;
            }
        }
        
        // Safe to load - no conflict detected
        require_once $autoloadPath;
    }
}