<?php
/**
 * @package     MefisAssetBridge
 * @subpackage  MefisAssetBridgeCore
 *
 * @copyright   Copyright (C) 2025 Goran Usljebrka. All rights reserved.
 * @license     Proprietary — No resale or incorporation into other projects without explicit written permission. see LICENSE.txt
 */

namespace MefisAssetBridgeCore;

/**
 * Main entry point for the MefisAssetBridgeCore
 */
class MefisAssetBridgeCore
{
    /** @var self|null Singleton instance */
    private static ?self $instance = null;

    /** @var bool Whether init() has already been run */
    private static bool $initialized = false;

    /** @var \MefisAssetBridgeCore\Api\ApiFactory|null Shared ApiFactory instance stored on the core */
    private ?\MefisAssetBridgeCore\Api\ApiFactory $apiFactory = null;

    /**
     * Make constructor private to enforce singleton usage.
     */
    private function __construct()
    {
        // run initialization (autoloader registration)
        self::init();
    }

    /**
     * Prevent cloning of the singleton
     */
    private function __clone()
    {
    }

    /**
     * Prevent unserialization of the singleton
     */
    public function __wakeup()
    {
    }
    /**
     * Initialize the core functionality
     *
     * @return void
     */
    public static function init(): void
    {
        // Idempotent init: only register autoloader once
        if (self::$initialized) {
            return;
        }

        // Load Composer autoloader for vendor libraries
        // Note: Joomla has phpseclib3, but does NOT have full GuzzleHttp\Client
        // (only guzzlehttp/promises and guzzlehttp/psr7), so we need component's vendor
        $composerAutoload = __DIR__ . '/../lib/vendor/autoload.php';
        if (file_exists($composerAutoload)) {
            require_once $composerAutoload;
        }

        // Register autoloader
        spl_autoload_register(function ($class): void {
            // Only handle our namespace
            if (strpos($class, 'MefisAssetBridgeCore\\') !== 0) {
                return;
            }

            $classPath = str_replace('MefisAssetBridgeCore\\', '', $class);
            $classPath = str_replace('\\', DIRECTORY_SEPARATOR, $classPath);
            $filePath = __DIR__ . DIRECTORY_SEPARATOR . $classPath . '.php';

            if (file_exists($filePath)) {
                require_once $filePath;
            }
        });

        self::$initialized = true;
    }

    /**
     * Get an instance of the API factory
     *
     * @return Api\ApiFactory
     */
    /**
     * Return the shared ApiFactory instance. Will create it once and return the same
     * object on subsequent calls.
     *
     * @return Api\ApiFactory
     */
    public static function getApiFactory(): Api\ApiFactory
    {
        $core = self::getInstance();

        if ($core->apiFactory === null) {
            // Use ApiFactory's singleton accessor so all callers get the same object
            $core->apiFactory = Api\ApiFactory::getInstance();
        }

        return $core->apiFactory;
    }

    /**
     * Get singleton instance of the core. This will construct and initialize the core
     * only once.
     */
    public static function getInstance(): self
    {
        if (self::$instance === null) {
            self::$instance = new self();
        }

        return self::$instance;
    }

}

// Initialize the core
MefisAssetBridgeCore::init();