<?php
/**
 * @package     MefisAssetBridge
 * @subpackage  Joomla_Administrator_Component
 *
 * @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 Mefi\Component\MefisAssetBridge\Administrator\Traits;

defined('_JEXEC') or die;

use Joomla\CMS\Language\Text;
use Joomla\CMS\Log\Log as JoomlaLog;

use Mefi\Component\MefisAssetBridge\Administrator\Helper\CoreBridgeHelper;
use Mefi\Component\MefisAssetBridge\Administrator\Log\SensitiveDataFilter;
use Mefi\Component\MefisAssetBridge\Administrator\Traits\CoreBridge\AuthTrait;
use Mefi\Component\MefisAssetBridge\Administrator\Traits\CoreBridge\AssetTrait;
use Mefi\Component\MefisAssetBridge\Administrator\Traits\CoreBridge\CoreCacheTrait;
use MefisAssetBridgeCore\Log;

/**
 * Core Bridge Base Trait
 *
 * Base trait that includes other specialized traits for core API integration
 *
 * @since  1.0.0
 */
trait CoreBridgeTrait
{
    use AuthTrait;
    use AssetTrait;
    use CoreCacheTrait;

    /**
     * Get the API factory with base URL configured
     *
     * @return \MefisAssetBridgeCore\Api\ApiFactory
     *
     * @throws \RuntimeException If core is not loaded
     * @since  1.0.0
     */
    protected function getApiFactory()
    {
        if (!class_exists('MefisAssetBridgeCore\\MefisAssetBridgeCore')) {
            throw new \RuntimeException(Text::_('COM_MEFISASSETBRIDGE_ERROR_CORE_NOT_LOADED'));
        }

        // Initialize CMS-agnostic logger
        $log = \MefisAssetBridgeCore\Log::getInstance();
        $log->setLogger(function($message, $level, $category): void {
            // Gate: suppress debug messages unless Joomla's global debug mode is active.
            if ($level === 'debug' && !(\defined('JDEBUG') && JDEBUG)) {
                return;
            }

            $message = SensitiveDataFilter::filter($message);

            switch ($level) {
                case 'emergency':
                    $joomlaLevel = JoomlaLog::EMERGENCY;
                    break;
                case 'alert':
                    $joomlaLevel = JoomlaLog::ALERT;
                    break;
                case 'critical':
                    $joomlaLevel = JoomlaLog::CRITICAL;
                    break;
                case 'error':
                    $joomlaLevel = JoomlaLog::ERROR;
                    break;
                case 'warning':
                    $joomlaLevel = JoomlaLog::WARNING;
                    break;
                case 'notice':
                    $joomlaLevel = JoomlaLog::NOTICE;
                    break;
                case 'info':
                    $joomlaLevel = JoomlaLog::INFO;
                    break;
                case 'debug':
                    $joomlaLevel = JoomlaLog::DEBUG;
                    break;
                default:
                    $joomlaLevel = JoomlaLog::INFO;
                    break;
            }
            JoomlaLog::add($message, $joomlaLevel, $category);
        });

        return \MefisAssetBridgeCore\MefisAssetBridgeCore::getApiFactory()
            ->setBaseUrl(CoreBridgeHelper::getApiBaseUrl())
            ->setSSLCheck(CoreBridgeHelper::getSSLCheck())
            ->setComponentVersion(CoreBridgeHelper::getComponentVersion())
            ->setDeviceInfo($this->getDeviceInfo())
            ->setDomain(CoreBridgeHelper::getDomain())
            ->setCMSUserId(CoreBridgeHelper::getId())
            ->setCMSVersion(CoreBridgeHelper::getCMSVersion())
            ->setCMSType(CoreBridgeHelper::getCMSType())
            ->setKeystore(CoreBridgeHelper::getKeyStore());
    }
}