<?php
include_once __DIR__ . '/error-logger.php';

class Database
{
    private $host = "localhost";
    private $db_name = "hopepress_attendance";
    private $username = "root";
    private $password = "";
    public $conn;

    public function getConnection()
    {
        $this->conn = null;

        try {
            $this->conn = new PDO(
                "mysql:host=" . $this->host . ";dbname=" . $this->db_name,
                $this->username,
                $this->password
            );
            $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $this->conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
        } catch (PDOException $e) {
            ErrorLogger::logDatabaseError($e, 'Database Connection');
            echo json_encode([
                'success' => false,
                'message' => 'Connection Error: ' . $e->getMessage()
            ]);
            exit();
        } catch (Exception $e) {
            ErrorLogger::logError($e, ['context' => 'Database Connection']);
            echo json_encode([
                'success' => false,
                'message' => 'An unexpected error occurred: ' . $e->getMessage()
            ]);
            exit();
        }

        return $this->conn;
    }
}
