<?php
header("Content-Type: application/json");
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET");
header("Access-Control-Allow-Headers: Content-Type");

require_once "config.php";

$room_id = isset($_GET['room_id']) ? (int) $_GET['room_id'] : 0;

if ($room_id <= 0) {
    http_response_code(422);
    echo json_encode([
        "status" => false,
        "message" => "room_id is required"
    ]);
    exit;
}

try {
    $stmt = $pdo->prepare("
        SELECT 
            r.id,
            r.room_name,
            r.room_type,
            r.price_per_night,
            r.max_guests,
            r.description,
            h.id AS hotel_id,
            h.hotel_name,
            h.location
        FROM hotel_rooms r
        JOIN hotels h ON h.id = r.id
        WHERE r.id = ?
    ");
    $stmt->execute([$room_id]);
    $room = $stmt->fetch();

    if (!$room) {
        http_response_code(404);
        echo json_encode([
            "status" => false,
            "message" => "Room not found"
        ]);
        exit;
    }

    // Images
    $img = $pdo->prepare("SELECT image_path FROM hotel_room_images WHERE room_id = ?");
    $img->execute([$room_id]);
    $room['images'] = $img->fetchAll(PDO::FETCH_COLUMN);

    // Features
    $feat = $pdo->prepare("SELECT feature FROM room_features WHERE room_id = ?");
    $feat->execute([$room_id]);
    $room['features'] = $feat->fetchAll(PDO::FETCH_COLUMN);

    echo json_encode([
        "status" => true,
        "room" => $room
    ]);

} catch (PDOException $e) {
    http_response_code(500);
    echo json_encode([
        "status" => false,
        "message" => "Server error"
    ]);
}
