<?php
// Required headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");

// Include database and object files
include_once '../config/database.php'; // Include the database connection
include_once '../objects/room.php'; // Include the Room object
include_once '../helpers/rental_metadata.php';

// Instantiate database
$database = new Database();
$db = $database->getConnection();

// Initialize room object
$room = new Room($db);

// Get the incoming data
$data = json_decode(file_get_contents("php://input"));

// Check if ID is set
if (isset($data->id)) {
    // Set the room ID to fetch
    $room->id = $data->id;

    // Call the method to get room details
    $room->getOneRow();

    // Prepare the room data array
    $vehicle_meta = parse_vehicle_metadata($room->remark);
    $room_data = array(
        "id" => $room->id,
        "room_number" => $room->room_number, // Ensure these properties exist in your Room class
        "room_type" => $room->room_type,
        "bed_type" => $room->bed_type,
        "max_occupancy" => $room->max_occupancy,
        "room_rate" => $room->room_rate,
        "extra_bed" => $room->extra_bed,
        "extra_person" => $room->extra_person,
        "remark" => $vehicle_meta['note'],
        "vehicle_meta" => $vehicle_meta,
        // Add any additional fields you have for rooms
    );

    // Return the room data as JSON
    echo json_encode($room_data);
} else {
    // Return error if ID is not provided
    echo json_encode(array("message" => "Room ID is missing."));
}
?>
