<?php

declare(strict_types=1);

namespace apexl\Io\modules\logger\dto;

use apexl\Config\Configuration;
use apexl\Config\ConfigurationInterface;
use apexl\Io\modules\logger\enums\LogLevel;
use Exception;
use Spatie\DataTransferObject\DataTransferObject;
use function apexl\Io\config;

class LoggerConfig extends DataTransferObject
{
    public LogLevel $level;
    public bool $catchErrors = true;
    public bool $enableDynamicStreams = true;
    public bool $warnAboutDynamicStreams = false;
    public streamConfigCollection $streams;

    /**
     * @throws Exception
     */
    public static function fromConfig(ConfigurationInterface $config): LoggerConfig
    {
        return new LoggerConfig([
            'level' => $config->get('logger.level'),
            'catchErrors' => $config->get('logger.catch_errors'),
            'enableDynamicStreams' => $config->get('logger.enable_dynamic_streams'),
            'warnAboutDynamicStreams' => $config->get('logger.warn_about_dynamic_streams'),
            'streams' => new streamConfigCollection(
                array_map(
                    fn(array $streamConfig) => streamConfig::fromConfig(
                        $streamConfig,
                        $config->get('logger.level'),
                        $config->get('logger.directory')
                    ),
                    (array) $config->get('logger.streams'),
                )
            ),
        ]);
    }
}
