<?php
/**
 * API: Get Room Details
 * Returns room information for the modal popup
 */

// Enable error reporting for debugging
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Define base path
define('BASE_PATH', dirname(__DIR__));

// Load required files
require_once BASE_PATH . '/config/config.php';
require_once BASE_PATH . '/config/database.php';
require_once BASE_PATH . '/config/auth.php';

// Require authentication
require_auth();

// Set header to return JSON
header('Content-Type: application/json');

// Get room ID from request
$room_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if (!$room_id) {
    echo json_encode([
        'status' => 'error',
        'message' => 'Room ID is required'
    ]);
    exit;
}

try {
    // Get room details with current occupant information
    $room = db_fetch_one("
        SELECT 
            r.*,
            rt.type_name, rt.type_code, rt.base_price,
            CONCAT(g.first_name, ' ', g.last_name) as guest_name,
            g.guest_id,
            res.reservation_id, res.booking_code,
            res.check_in_date, res.check_out_date
        FROM rooms r
        JOIN room_types rt ON r.room_type_id = rt.room_type_id
        LEFT JOIN reservations res ON r.room_id = res.room_id AND res.booking_status = 'checked_in'
        LEFT JOIN guests g ON res.guest_id = g.guest_id
        WHERE r.room_id = ?
    ", [$room_id]);

    if ($room) {
        echo json_encode([
            'status' => 'success',
            'data' => $room
        ]);
    } else {
        echo json_encode([
            'status' => 'error',
            'message' => 'Room not found'
        ]);
    }
} catch (Exception $e) {
    error_log("API get-room.php error: " . $e->getMessage());
    echo json_encode([
        'status' => 'error',
        'message' => 'Database error: ' . $e->getMessage()
    ]);
}