<?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;

/**
 * CMS Agnostic Logging Bridge
 * Acts as a bridge to call Joomla's Log or WordPress's logging methods.
 * Singleton pattern for global access within the core.
 */
class Log
{
    /** @var Log|null Singleton instance */
    private static ?Log $instance = null;

    /** @var callable|null CMS-specific logging function */
    private $logger = null;

    /**
     * Private constructor to prevent direct instantiation
     */
    private function __construct() {}

    /**
     * Get singleton instance
     *
     * @return Log
     */
    public static function getInstance(): Log
    {
        if (self::$instance === null) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * Set the CMS-specific logging function
     * This should be called during coreBridge initialization
     *
     * @param callable $logger Function that accepts ($message, $level, $category)
     * @return void
     */
    public function setLogger(callable $logger): void
    {
        $this->logger = $logger;
    }

    /**
     * Log an emergency message
     *
     * @param string $message
     * @param string $category
     * @return void
     */
    public function emergency(string $message, string $category = 'default'): void
    {
        $this->log($message, 'emergency', $category);
    }

    /**
     * Log an alert message
     *
     * @param string $message
     * @param string $category
     * @return void
     */
    public function alert(string $message, string $category = 'default'): void
    {
        $this->log($message, 'alert', $category);
    }

    /**
     * Log a critical message
     *
     * @param string $message
     * @param string $category
     * @return void
     */
    public function critical(string $message, string $category = 'default'): void
    {
        $this->log($message, 'critical', $category);
    }

    /**
     * Log an error message
     *
     * @param string $message
     * @param string $category
     * @return void
     */
    public function error(string $message, string $category = 'default'): void
    {
        $this->log($message, 'error', $category);
    }

    /**
     * Log a warning message
     *
     * @param string $message
     * @param string $category
     * @return void
     */
    public function warning(string $message, string $category = 'default'): void
    {
        $this->log($message, 'warning', $category);
    }

    /**
     * Log a notice message
     *
     * @param string $message
     * @param string $category
     * @return void
     */
    public function notice(string $message, string $category = 'default'): void
    {
        $this->log($message, 'notice', $category);
    }

    /**
     * Log an info message
     *
     * @param string $message
     * @param string $category
     * @return void
     */
    public function info(string $message, string $category = 'default'): void
    {
        $this->log($message, 'info', $category);
    }

    /**
     * Log a debug message
     *
     * @param string $message
     * @param string $category
     * @return void
     */
    public function debug(string $message, string $category = 'default'): void
    {
        $this->log($message, 'debug', $category);
    }

    /**
     * Internal logging method
     *
     * @param string $message
     * @param string $level
     * @param string $category
     * @return void
     */
    private function log(string $message, string $level, string $category): void
    {
        if ($this->logger !== null) {
            call_user_func($this->logger, $message, $level, $category);
        }
    }
}