<?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"));

if (empty($data->id) || empty($data->status)) {
    sendResponse(400, 'error', 'Enquiry ID and status are required');
}

// Validate against the specific ENUM values for the enquiries table
$valid_statuses = ['unread', 'read', 'replied'];
if (!in_array($data->status, $valid_statuses)) {
    sendResponse(400, 'error', 'Invalid status value. Must be unread, read, or replied.');
}

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

try {
    $query = "UPDATE package_enquiries SET status = :status WHERE id = :id";
    $stmt = $db->prepare($query);
    
    $stmt->execute([
        ':status' => $data->status,
        ':id' => $data->id
    ]);

    if ($stmt->rowCount() > 0) {
        sendResponse(200, 'success', 'Enquiry status updated successfully');
    } else {
        sendResponse(404, 'error', 'Enquiry not found or status is already the same');
    }
} catch (PDOException $e) {
    sendResponse(500, 'error', 'Database error: ' . $e->getMessage());
}
?>