<?php

declare(strict_types=1);

namespace Apexl\Io\Logger\Dtos;

use Apexl\Config\ConfigurationInterface;
use Apexl\Io\Logger\Enums\LogLevel;
use Exception;
use Spatie\DataTransferObject\DataTransferObject;

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'),
                )
            ),
        ]);
    }
}
