<?php
/**
 * Database Configuration File
 * Hotel Booking System - Almaris
 */

// Database Credentials
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', '');
define('DB_NAME', 'hotel_booking_db');

// Create Connection using MySQLi
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

// Check Connection
if ($conn->connect_error) {
    // Log error for debugging
    error_log("Database Connection Error: " . $conn->connect_error, 3, __DIR__ . '/db_errors.log');
    die('Database connection failed. Please contact administrator.');
}

// Set charset to UTF-8
$conn->set_charset("utf8");

// Optional: Define commonly used queries in a class for reusability
class Database {
    private $conn;
    
    public function __construct($connection) {
        $this->conn = $connection;
    }
    
    /**
     * Prepare and Execute Query
     */
    public function query($sql, $types = '', $params = []) {
        if ($stmt = $this->conn->prepare($sql)) {
            if (!empty($params)) {
                $stmt->bind_param($types, ...$params);
            }
            $stmt->execute();
            return $stmt;
        } else {
            error_log("Query Error: " . $this->conn->error, 3, __DIR__ . '/db_errors.log');
            return false;
        }
    }
    
    /**
     * Get Connection Object
     */
    public function getConnection() {
        return $this->conn;
    }
    
    /**
     * Close Connection
     */
    public function close() {
        $this->conn->close();
    }
}

// Instantiate Database class for use in pages
$db = new Database($conn);

?>
