<?php
session_start();
header('Content-Type: application/json');
require_once __DIR__ . '/config/db.php';
require_once __DIR__ . '/includes/webpush.php';
if (empty($_SESSION['user_id'])) { http_response_code(401); echo json_encode(['ok' => false]); exit; }
dabil_push_ensure_table($conn);
$uid = (int)$_SESSION['user_id'];

$body     = json_decode(file_get_contents('php://input'), true);
$endpoint = $body['endpoint'] ?? '';
if ($endpoint) {
    $d = $conn->prepare("DELETE FROM push_subscriptions WHERE user_id=? AND endpoint=?");
    $d->bind_param("is", $uid, $endpoint);
    $d->execute();
} else {
    $d = $conn->prepare("DELETE FROM push_subscriptions WHERE user_id=?");
    $d->bind_param("i", $uid);
    $d->execute();
}
echo json_encode(['ok' => true]);
