<?php
header('Content-Type: application/json');
$base = realpath(__DIR__ . '/../../../');
if (file_exists($base . '/app/session.php')) include $base . '/app/session.php';
if (file_exists($base . '/config/db.php')) include $base . '/config/db.php';
if (!isset($link)) { http_response_code(500); echo json_encode(['ok'=>false,'error'=>'db']); exit; }

session_start();
if (!isset($_SESSION['admin_id'])) { http_response_code(403); echo json_encode(['ok'=>false,'error'=>'not_admin']); exit; }

$body = json_decode(file_get_contents('php://input'), true);
$id = isset($body['id']) ? intval($body['id']) : 0;
if ($id <= 0) { http_response_code(400); echo json_encode(['ok'=>false,'error'=>'id_required']); exit; }

$stmt = mysqli_prepare($link, "DELETE FROM token_contracts WHERE id = ?");
mysqli_stmt_bind_param($stmt, 'i', $id);
$ok = mysqli_stmt_execute($stmt);
if ($ok) echo json_encode(['ok'=>true]); else echo json_encode(['ok'=>false,'error'=>'delete_failed']);

?>