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

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

$email = isset($_GET['email']) ? $_GET['email'] : '';

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

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

try {
    $stmt = $db->prepare("SELECT * FROM hotel_registrations WHERE email = :email LIMIT 1");
    $stmt->execute([':email' => $email]);
    
    if ($stmt->rowCount() > 0) {
        $hotel = $stmt->fetch(PDO::FETCH_ASSOC);
        
        // Map database columns to the format the React component expects
        $formatted = [
            'id' => $hotel['id'],
            'ownerName' => $hotel['owner_name'],
            'email' => $hotel['email'],
            'phone' => $hotel['owner_contact'],
            'hotelName' => $hotel['hotel_name'],
            'hotelAddress' => $hotel['hotel_address'],
            'city' => $hotel['city'],
            'state' => $hotel['state'],
            'pincode' => $hotel['pincode'],
            'status' => ucfirst($hotel['status']), // Capitalizes status
            
            // Profile & Rooms
            'starRating' => $hotel['star_rating'] ? (int)$hotel['star_rating'] : 3,
            'totalRooms' => $hotel['total_rooms'] ? (int)$hotel['total_rooms'] : 10,
            'description' => $hotel['description'] ?: '',
            'amenities' => json_decode($hotel['amenities'], true) ?: [],
            'roomTypes' => json_decode($hotel['room_types'], true) ?: []
        ];
        
        sendResponse(200, 'success', 'Profile fetched', $formatted);
    } else {
        sendResponse(404, 'error', 'Hotel not found');
    }
} catch (PDOException $e) {
    sendResponse(500, 'error', 'Database error: ' . $e->getMessage());
}
?>