<?php
require_once '../config/headers.php';
require_once '../config/database.php';
require_once '../utils/response.php';

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    sendResponse(405, 'error', 'Method not allowed');
}

$data = json_decode(file_get_contents("php://input"), true);

if (empty($data['email'])) {
    sendResponse(400, 'error', 'Email is required');
}

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

try {
    $query = "UPDATE hotel_registrations SET 
                star_rating = :star,
                total_rooms = :rooms,
                description = :desc,
                amenities = :amenities,
                room_types = :room_types
              WHERE email = :email";
              
    $stmt = $db->prepare($query);
    $stmt->execute([
        ':star' => isset($data['starRating']) ? (int)$data['starRating'] : 3,
        ':rooms' => isset($data['totalRooms']) ? (int)$data['totalRooms'] : 10,
        ':desc' => isset($data['description']) ? $data['description'] : '',
        ':amenities' => isset($data['amenities']) ? json_encode($data['amenities']) : json_encode([]),
        ':room_types' => isset($data['roomTypes']) ? json_encode($data['roomTypes']) : json_encode([]),
        ':email' => $data['email']
    ]);

    sendResponse(200, 'success', 'Profile updated successfully');
} catch (PDOException $e) {
    sendResponse(500, 'error', 'Database error: ' . $e->getMessage());
}
?>