<?php

/**
 * Magento Simulation Environment
 * Creates mock classes and functions to simulate Magento behavior for testing
 */

// Mock Magento Framework classes
if (!class_exists('\Magento\Framework\App\ObjectManager')) {

    // Mock ObjectManager
    class MockObjectManager
    {
        private static $instance;

        public static function getInstance()
        {
            if (!self::$instance) {
                self::$instance = new self();
            }
            return self::$instance;
        }

        public function get($className)
        {
            switch ($className) {
                case 'Magento\Customer\Model\Session':
                    return new MockCustomerSession();
                default:
                    return new stdClass();
            }
        }
    }

    // Mock Customer Session
    class MockCustomerSession
    {
        public function isLoggedIn()
        {
            // Simulate different user scenarios based on URL parameters
            return isset($_GET['user_type']) && $_GET['user_type'] === 'logged_in';
        }

        public function getCustomer()
        {
            return new MockCustomer();
        }
    }

    // Mock Customer
    class MockCustomer
    {
        public function getId()
        {
            return 'magento_customer_123';
        }

        public function getEmail()
        {
            return 'customer@example.com';
        }

        public function getData($field)
        {
            switch ($field) {
                case 'telephone':
                    return '+40123456789';
                default:
                    return '';
            }
        }

        public function getFirstname()
        {
            return 'John';
        }

        public function getLastname()
        {
            return 'Doe';
        }
    }

    // Mock Template Context (for $this->getViewFileUrl)
    class MockTemplateContext
    {
        public function getViewFileUrl($path)
        {
            // Convert Magento view file path to our static file path
            if (strpos($path, 'eSky_VueLicitatii::css/') === 0) {
                $file = str_replace('eSky_VueLicitatii::css/', '', $path);
                return '/pub/static/vue-licitatii/' . $file;
            }
            if (strpos($path, 'eSky_VueLicitatii::js/') === 0) {
                $file = str_replace('eSky_VueLicitatii::js/', '', $path);
                return '/pub/static/vue-licitatii/' . $file;
            }
            return '/pub/static/vue-licitatii/' . basename($path);
        }
    }

    // Create namespace aliases
    class_alias('MockObjectManager', '\Magento\Framework\App\ObjectManager');

    // Create global template context
    global $templateContext;
    if (!isset($templateContext)) {
        $templateContext = new MockTemplateContext();
    }
}

// Helper function to simulate asset URLs
function getAssetUrl($path)
{
    return '/pub/static/vue-licitatii/' . $path;
}

// Helper function for template context
function getViewFileUrl($path)
{
    global $templateContext;
    return $templateContext ? $templateContext->getViewFileUrl($path) : '/pub/static/vue-licitatii/' . basename($path);
}

// Add some helpful debug info
if (isset($_GET['debug'])) {
    echo "<!-- Magento Simulation Active -->\n";
    echo "<!-- User Type: " . ($_GET['user_type'] ?? 'guest') . " -->\n";
    echo "<!-- Available params: ?user_type=logged_in or ?user_type=guest -->\n";
}
