<?php

declare(strict_types=1);

namespace Apexl\Io\Logger\Services\HandlerCreator;

use Apexl\Io\Logger\Dtos\HandlerConfig;
use Apexl\Io\Logger\Dtos\StreamConfig;
use Apexl\Vault\Vault;
use Monolog\Handler\HandlerInterface;
use MySQLHandler\MySQLHandler;

final readonly class DatabaseLogHandlerCreator extends HandlerCreator
{
    public function __construct(private Vault $db) {}

    public function create(
        string $streamName,
        HandlerConfig $handlerConfig,
        StreamConfig $streamConfig
    ): HandlerInterface {
        $connection = $this->db->connection()->handle();

        return new MySQLHandler(
            $connection,
            $handlerConfig->params->table,
            [],
            $this->level($handlerConfig, $streamConfig),
        );
    }
}
