<?php
require_once __DIR__ . '/../../connect.php';

$id = filterRequest("id");

$fields = [];
$params = [':id' => $id];

$mapping = [
    "title"        => "title",
    "body"         => "body",
    "passengerID"  => "passenger_id",
    "isShown"      => "isShown",
    "updatedAt"    => "updated_at"
];

// تجهيز الـ SET والأرقام المقابلة
foreach ($mapping as $requestKey => $dbColumn) {
    if (isset($_POST[$requestKey])) {
        $value = filterRequest($requestKey);
        $fields[] = "`$dbColumn` = :$requestKey";
        $params[":$requestKey"] = $value;
    }
}

if (empty($fields)) {
    jsonError("No fields to update");
    exit;
}

$setClause = implode(", ", $fields);
$sql = "UPDATE `notifications` SET $setClause WHERE `id` = :id";

$stmt = $con->prepare($sql);
$stmt->execute($params);

if ($stmt->rowCount() > 0) {
    jsonSuccess(null, "Notification data updated successfully");
} else {
    jsonError("Failed to update notification data");
}
?>