<?php
    namespace AxiumPHP\Core\Database\Drivers;

    use PDO;
    use PDOException;
    use RuntimeException;

    class MySQL extends PDOAbstract {
        protected function connect(string $envName): void {
            $host = $_ENV["{$envName}_DATABASE_HOST"];
            $port = $_ENV["{$envName}_DATABASE_PORT"] ?? 3306;
            $dbname = $_ENV["{$envName}_DATABASE_NAME"];
            $user = $_ENV["{$envName}_DATABASE_USERNAME"];
            $password = $_ENV["{$envName}_DATABASE_PASSWORD"];
            $charset = $_ENV["{$envName}_DATABASE_CHARSET"] ?? 'utf8mb4';

            try {
                $this->connection = new PDO(
                    dsn: "mysql:host={$host};port={$port};dbname={$dbname};charset={$charset}",
                    username: $user,
                    password: $password,
                    options: [
                        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
                        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
                        PDO::ATTR_PERSISTENT => true,
                    ]
                );
            } catch (PDOException $e) {
                throw new RuntimeException(message: "Erro ao conectar no MySQL: {$e->getMessage()}");
            }
        }
    }