<?php
require_once __DIR__ . '/../includes/db.php';

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    json_response(['ok' => false, 'error' => 'Doar POST'], 405);
}
if (!csrf_check($_POST['csrf_token'] ?? null)) {
    json_response(['ok' => false, 'error' => 'CSRF invalid'], 403);
}

$id = (int)($_POST['id'] ?? 0);
if ($id <= 0) {
    json_response(['ok' => false, 'error' => 'ID invalid'], 422);
}

try {
    $affected = db_exec("DELETE FROM name_templates WHERE id = ?", [$id]);
    json_response(['ok' => true, 'deleted' => $affected]);
} catch (Throwable $ex) {
    json_response(['ok' => false, 'error' => $ex->getMessage()], 500);
}
