<?php
class Database {
    private $host = "localhost";
    private $db_name = "appdeve1_certificate";  
    private $username = "appdeve1_certificate";
    private $password = "Certificate@11";
    public $conn;

    // Static property to hold the single instance
    private static $instance = null;
    
    // Private constructor to prevent direct creation of object
    private function __construct() {
        $this->conn = null;
        try {
            // Attempt connection using PDO
            $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
            $this->conn->exec("set names utf8");
            // Set error mode to throw exceptions on failure
            $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        } catch(PDOException $exception) {
            // Log the connection error and set conn to null to signal failure
            error_log("DATABASE CONNECTION ERROR: " . $exception->getMessage());
            $this->conn = null;
        }
    }

    // Public method to get the single instance (Singleton)
    public static function getInstance() {
        if (self::$instance == null) {
            self::$instance = new Database();
        }
        return self::$instance;
    }
    
    // Public method to get the connection object, matching the expected method in index.php
    public function getConn() {
        return $this->conn;
    }
}
?>