<?php
require_once 'config.php';

checkAccess(['admin', 'programmer']);

$data = $REQUEST_DATA;

if (!isset($data['id'])) response(["error" => "ID faltante"], 400);

try {
    // 1. Rescatar datos para el log antes de borrar
    $stmtInfo = $pdo->prepare("SELECT description, date, amount_bs, reference FROM bank_transactions WHERE id = ?");
    $stmtInfo->execute([$data['id']]);
    $tx = $stmtInfo->fetch();

    if ($tx) {
        $stmt = $pdo->prepare("DELETE FROM bank_transactions WHERE id = ?");
        $stmt->execute([$data['id']]);
        
        // Log con lujo de detalles
        $msg = "ELIMINÓ movimiento bancario: " . $tx['description'] . " de fecha " . $tx['date'] . " por Bs." . number_format($tx['amount_bs'], 2, ',', '.') . " Ref:" . ($tx['reference'] ?? 'S/R');
        logSystemAction($msg, "BANCO");
        
        response(["success" => true]);
    } else {
        response(["error" => "Registro no encontrado"], 404);
    }
} catch (PDOException $e) {
    response(["error" => $e->getMessage()], 500);
}
?>