<?php

namespace EmblueNewsletter\Log;

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

/**
 * Static logging facade backed by Monolog.
 * Writes to /wp-content/plugins/mdirector-newsletter/log/mdirector-newsletter.log
 */
class Log
{
    private static ?Logger $logger = null;

    private static function getLogger(): Logger
    {
        if (self::$logger === null) {
            $logDir = __DIR__ . '/../../log';

            if (!is_dir($logDir)) {
                wp_mkdir_p($logDir);
            }

            self::$logger = new Logger('mdirector-newsletter');
            self::$logger->pushHandler(
                new StreamHandler($logDir . '/mdirector-newsletter.log', Logger::DEBUG)
            );
        }

        return self::$logger;
    }

    public static function info(string $message, $context = []): void
    {
        self::getLogger()->info($message, is_array($context) ? $context : ['data' => $context]);
    }

    public static function notice(string $message, $context = []): void
    {
        self::getLogger()->notice($message, is_array($context) ? $context : ['data' => $context]);
    }

    public static function error(string $message, $context = []): void
    {
        self::getLogger()->error($message, is_array($context) ? $context : ['data' => $context]);
    }

    public static function debug(string $message, $context = []): void
    {
        self::getLogger()->debug($message, is_array($context) ? $context : ['data' => $context]);
    }
}
