<?php
$base = realpath(__DIR__ . '/../../');
if (file_exists($base . '/config/db.php')) include $base . '/config/db.php';
if (file_exists($base . '/config/deposit_lib.php')) include $base . '/config/deposit_lib.php';
if (!isset($link)) { echo "DB not available\n"; exit(1); }

$rpc = [];
if (file_exists($base . '/config/rpc.php')) $rpc = include $base . '/config/rpc.php';

deposit_ensure_table($link);

$res = mysqli_query($link, "SELECT * FROM deposits WHERE status = 'pending' ORDER BY created_at ASC LIMIT 50");
if (!$res) { echo "No pending deposits or query failed\n"; exit(0); }

while ($d = mysqli_fetch_assoc($res)) {
    echo "Checking deposit id={$d['id']} tx={$d['tx_hash']} coin={$d['coin']}\n";
    $result = deposit_verify_and_credit($link, $d, $rpc);
    echo "  status=" . $result['status'] . (isset($result['reason']) ? " ({$result['reason']})" : '') . "\n";
}

echo "Done.\n";
