<?php

declare(strict_types=1);

namespace App\Framework\Database\Driver;

final readonly class DriverConfig
{
    public function __construct(
        public DriverType $driverType,
        public string $host,
        public int $port,
        public string $database,
        public string $username,
        public string $password,
        public string $charset,
        public int $weight = 100,
        public int $maxConnections = 100
    ) {
    }

    public static function fromArray(array $config): DriverConfig
    {
        return new self(
            driverType: DriverType::tryFrom($config['driver']) ?? DriverType::MYSQL,
            host: $config['host'],
            port: $config['port'],
            database: $config['database'],
            username: $config['username'],
            password: $config['password'],
            charset: $config['charset'] ?? 'utf8mb4',
            weight: $config['weight'] ?? 100,
            maxConnections: $config['max_connections'] ?? 100
        );
    }
}
