<?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');
}

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

try {
    $query = "SELECT * FROM hotel_registrations ORDER BY created_at DESC";
    $stmt = $db->prepare($query);
    $stmt->execute();
    
    $registrations = $stmt->fetchAll(PDO::FETCH_ASSOC);

    // Map database columns to the camelCase format expected by React
    $formatted_data = array_map(function($r) {
        return [
            'id' => $r['id'],
            'ownerName' => $r['owner_name'],
            'ownerContact' => $r['owner_contact'],
            'propertyManagerName' => $r['property_manager_name'],
            'propertyManagerPhone' => $r['property_manager_phone'],
            'email' => $r['email'],
            'hotelName' => $r['hotel_name'],
            'gst' => $r['gst_number'],
            'hotelRegistrationNumber' => $r['registration_number'],
            'fireSafetyNoc' => $r['fire_safety_noc'],
            'cctvCamera' => $r['cctv_camera'],
            'bankDetails' => $r['bank_details'],
            'location' => $r['location_link'],
            'hotelAddress' => $r['hotel_address'],
            'city' => $r['city'],
            'state' => $r['state'],
            'pincode' => $r['pincode'],
            // Decode JSON arrays for images
            'roomPics' => json_decode($r['room_pics'], true) ?: [],
            'receptionPics' => json_decode($r['reception_pics'], true) ?: [],
            'bathroomPics' => json_decode($r['bathroom_pics'], true) ?: [],
            'interiorExteriorPics' => json_decode($r['interior_exterior_pics'], true) ?: [],
            // Capitalize status (e.g., 'pending' -> 'Pending')
            'status' => ucfirst($r['status']),
            'createdAt' => $r['created_at']
        ];
    }, $registrations);

    sendResponse(200, 'success', 'Data fetched successfully', $formatted_data);

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