<?php

namespace Oney\ThreeByFour\Logger;

use Oney\ThreeByFour\Helper\Config;

/**
 * Custom Logger
 * 
 * Extended Monolog logger with conditional logging based on configuration.
 * Only logs messages when logging is enabled in module configuration.
 * 
 * @package Oney\ThreeByFour\Logger
 * @version 1.0.0
 * @author Oney
 * @since 1.0.0
 */
class Logger extends \Monolog\Logger
{
    /**
     * Configuration helper instance
     * 
     * @var Config
     * @version 1.0.0
     */
    protected $_config;

    /**
     * Logger constructor
     * 
     * @param Config $config Configuration helper
     * @param string $name Logger name
     * @param array $handlers Log handlers
     * @param array $processors Log processors
     * @version 1.0.0
     */
    public function __construct(
        Config $config,
        string $name,
        array $handlers = [],
        array $processors = []
    ) {
        $this->_config = $config;
        parent::__construct($name, $handlers, $processors);
    }

    /**
     * Log info message
     * 
     * @param string $message Log message
     * @param array $context Log context data
     * @return void
     * @version 1.0.0
     */
    public function info($message, $context = []): void
    {
        $this->logIfActive('info', $message, $context);
    }

    /**
     * Log debug message
     * 
     * @param string $message Log message
     * @param array $context Log context data
     * @return void
     * @version 1.0.0
     */
    public function debug($message, $context = []): void
    {
        $this->logIfActive('debug', $message, $context);
    }

    /**
     * Log error message
     * 
     * @param string $message Log message
     * @param array $context Log context data
     * @return void
     * @version 1.0.0
     */
    public function error($message, $context = []): void
    {
        $this->logIfActive('error', $message, $context);
    }

    /**
     * Log warning message
     * 
     * @param string $message Log message
     * @param array $context Log context data
     * @return void
     * @version 1.0.0
     */
    public function warning($message, $context = []): void
    {
        $this->logIfActive('warning', $message, $context);
    }

    /**
     * Log message only if logging is active
     * 
     * @param string $level Log level
     * @param string $message Log message
     * @param array $context Log context data
     * @return void
     * @version 1.0.0
     */
    private function logIfActive(string $level, string $message, array $context = []): void
    {
        if ($this->_config->getGeneralConfigValue('log_active')) {
            parent::{$level}($message, $context);
        }
    }
}
