<?php
declare(strict_types=1);

require_once __DIR__ . '/../includes/audit.php';

class DataBase{
    
    private $host = 'localhost';
    private $user = "root";
    private $password = "";
    private $dbname = 'cars';
    
    private ?PDO $conn = null;

    public function getConnection(): PDO
    {
        if ($this->conn === null) {
            try {
                $dsn = "mysql:host={$this->host};dbname={$this->dbname};charset=utf8;port=3306";

            $this->conn = new PDO($dsn, $this->user, $this->password, [
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
                PDO::ATTR_EMULATE_PREPARES => false,
                PDO::ATTR_STRINGIFY_FETCHES => false,
                PDO:: ATTR_DEFAULT_FETCH_MODE => PDO:: FETCH_ASSOC
            ]);
            audit_set_pdo_context($this->conn);
            } catch (PDOException $e) {
                echo "خطأ في الإتصال بقاعدة البيانات: ". $e->getMessage();
            }
        }
        return $this->conn;
    }
}
// $database = new DataBase();

// $conn = $database->getConnection();

// $sql = $conn->prepare("Select * from branches");
// $sql->execute();
// if($sql->rowCount() > 0 ){
//     while($row = $sql->fetch()) {
//         echo $row['b_name'] . " " . $row['b_boss'] . "<br \>";
//     }
// }

