<?php

declare(strict_types=1);

namespace App\Logger;

use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Monolog\Processor\IntrospectionProcessor;
use Monolog\Processor\WebProcessor;
use Psr\Log\LoggerInterface;

final class LoggerFactory
{
    /**
     * @param array<string, mixed> $config
     */
    public static function create(array $config): LoggerInterface
    {
        $name = 'bot';
        $debug = (bool) ($config['app.debug'] ?? false);
        $path = __DIR__ . '/../../storage/logs/app.log';
        $dir = dirname($path);
        if (!is_dir($dir)) {
            mkdir($dir, 0777, true);
        }
        $logger = new Logger($name);
        $logger->pushHandler(new StreamHandler($path, $debug ? Logger::DEBUG : Logger::INFO));
        $logger->pushHandler(new StreamHandler('php://stderr', $debug ? Logger::DEBUG : Logger::INFO));
        $logger->pushProcessor(new WebProcessor());
        return $logger;
    }
}
