<?php
header('Content-Type: application/json');
require_once __DIR__ . '/../../../shared/utils/helpers.php';
require_once __DIR__ . '/../../../shared/auth/jwt.php';
require_once __DIR__ . '/../../../shared/database/connection.php';

// Verificar autenticación
$userData = JWTAuth::authenticate();
if (!$userData) {
    jsonResponse(['success' => false, 'error' => 'No autenticado'], 401);
}

// Verificar permiso
if (!hasPermission('manage_recipients', 'telegram')) {
    jsonResponse(['success' => false, 'error' => 'No tienes permiso para eliminar destinatarios de Telegram.'], 403);
}

$input = json_decode(file_get_contents('php://input'), true);
$id = $input['id'] ?? null;

if (!$id) {
    jsonResponse(['success' => false, 'error' => 'ID requerido'], 400);
}

try {
    $db = getDB();
    $stmt = $db->prepare("DELETE FROM destinatarios_telegram WHERE id = ?");
    $stmt->execute([$id]);
    
    jsonResponse(['success' => true]);
} catch (PDOException $e) {
    jsonResponse(['success' => false, 'error' => $e->getMessage()], 500);
}
