<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: GET, DELETE");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

include_once 'db_connect.php';

$method = $_SERVER['REQUEST_METHOD'];

// 1. GET: Fetch all enquiries
if ($method == 'GET') {
    $query = "SELECT * FROM enquiries ORDER BY submitted_at DESC";
    $result = $conn->query($query);
    
    $enquiries = [];
    while($row = $result->fetch_assoc()) {
        $enquiries[] = $row;
    }
    echo json_encode(["success" => true, "data" => $enquiries]);
}

// 2. DELETE: Remove an enquiry
if ($method == 'DELETE') {
    $id = isset($_GET['id']) ? intval($_GET['id']) : 0;
    
    if ($id > 0) {
        $query = "DELETE FROM enquiries WHERE id = $id";
        if ($conn->query($query)) {
            echo json_encode(["success" => true, "message" => "Enquiry deleted."]);
        } else {
            http_response_code(500);
            echo json_encode(["success" => false, "message" => "Delete failed."]);
        }
    }
}

$conn->close();
?>