<?php
require_once '../includes/config.php';
require_once '../includes/dingconnect.php';
requireAdmin();
$db = getDB();

// Test balance
$action = $_GET['action'] ?? '';
$result = null;

if ($action === 'balance') {
    $result = dingGetBalance();
}

// Last 10 ding api_logs
$logs = [];
try {
    $logs = $db->query("SELECT * FROM api_logs WHERE operator LIKE 'ding_%' ORDER BY id DESC LIMIT 10")->fetchAll();
} catch(Exception $e) {}

// Last 5 international transactions
$txns = [];
try {
    $txns = $db->query("SELECT t.*, u.name as uname FROM transactions t JOIN users u ON t.user_id=u.id WHERE t.type='international' ORDER BY t.created_at DESC LIMIT 5")->fetchAll();
} catch(Exception $e) {}

$apiKey  = getSetting('ding_api_key') ?: '';
$mode    = getSetting('ding_mode') ?: 'manual';
$configured = dingIsConfigured();
?>
<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"><title>Ding Debug</title>
<link rel="stylesheet" href="../assets/css/admin.css">
<style>
.card{background:white;border-radius:12px;padding:20px;margin-bottom:16px;box-shadow:0 2px 8px rgba(0,0,0,.06)}
pre{background:#f5f5f5;padding:12px;border-radius:8px;font-size:12px;overflow:auto;white-space:pre-wrap;word-break:break-all}
.ok{color:green;font-weight:700} .err{color:red;font-weight:700}
</style>
</head>
<body>
<?php include '../includes/admin_sidebar.php'; ?>
<div class="main"><div class="content">
<h2>🔌 Ding Debug</h2>

<div class="card">
    <h3>Configuration</h3>
    <p>API Key: <b><?= $apiKey ? substr($apiKey,0,8).'...' : '<span class="err">NOT SET</span>' ?></b></p>
    <p>Mode: <b><?= $mode ?></b></p>
    <p>Configured: <b class="<?= $configured?'ok':'err' ?>"><?= $configured?'YES':'NO' ?></b></p>
    <a href="?action=balance" style="padding:8px 16px;background:#1a3c8f;color:white;border-radius:8px;text-decoration:none;display:inline-block;margin-top:10px">Test Balance</a>
</div>

<?php if($result): ?>
<div class="card">
    <h3>Balance Response</h3>
    <pre><?= htmlspecialchars(json_encode($result, JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE)) ?></pre>
</div>
<?php endif; ?>

<div class="card">
    <h3>Last API Logs (<?= count($logs) ?>)</h3>
    <?php if(empty($logs)): ?>
    <p class="err">No logs found — api_logs table may not exist or no calls made</p>
    <?php else: foreach($logs as $l): ?>
    <div style="border-bottom:1px solid #f0f0f0;padding:10px 0">
        <b><?= htmlspecialchars($l['operator']) ?></b> | HTTP <?= $l['status_code'] ?> | <?= $l['created_at'] ?>
        <pre><?= htmlspecialchars(substr($l['response_data'],0,500)) ?></pre>
    </div>
    <?php endforeach; endif; ?>
</div>

<div class="card">
    <h3>Last International Transactions (<?= count($txns) ?>)</h3>
    <?php if(empty($txns)): ?>
    <p>No international transactions yet</p>
    <?php else: foreach($txns as $t): ?>
    <div style="border-bottom:1px solid #f0f0f0;padding:10px 0;font-size:13px">
        <b><?= htmlspecialchars($t['uname']) ?></b> | <?= $t['phone_number'] ?> | ৳<?= $t['amount'] ?> | 
        <span style="color:<?= $t['status']==='success'?'green':'red' ?>"><?= $t['status'] ?></span><br>
        Note: <?= htmlspecialchars($t['note']) ?>
    </div>
    <?php endforeach; endif; ?>
</div>

</div></div>
</body>
</html>
