<?php
/**
 * debug_bonus_status.php
 * ⚠️ শুধু Debug-এর জন্য — Mobile-Number দিয়ে, Bonus-System-এর
 * প্রতিটা ধাপের বর্তমান অবস্থা দেখানোর জন্য
 *
 * ব্যবহার: debug_bonus_status.php?mobile=তোমার_মোবাইল_নাম্বার
 */
include __DIR__ . "/deepanshu/conn.php";
header('Content-Type: text/plain; charset=utf-8');
if (isset($conn)) { $conn->set_charset("utf8mb4"); }

$mobile = trim($_GET['mobile'] ?? '');
if ($mobile === '') {
    echo "ব্যবহার: debug_bonus_status.php?mobile=তোমার_মোবাইল_নাম্বার\n";
    exit;
}

// ✅ Mobile-Number দিয়ে, আসল User-ID খুঁজে বের করা
$mobileEsc = mysqli_real_escape_string($conn, $mobile);
$userQ = $conn->query("SELECT id, mobile FROM shonu_subjects WHERE mobile = '$mobileEsc' LIMIT 1");
if (!$userQ || $userQ->num_rows === 0) {
    echo "❌ এই Mobile-Number ('$mobile') দিয়ে কোনো User পাওয়া যায়নি।\n";
    exit;
}
$userRow = $userQ->fetch_assoc();
$uid = $userRow['id'];

echo "=== পাওয়া গেছে — User-ID: $uid (Mobile: {$userRow['mobile']}) ===\n\n";

echo "=== ধাপ ১ — thevani Table (Deposit-History) ===\n";
$dep = $conn->query("SELECT id, motta, sthiti, dinankavannuracisi FROM thevani WHERE balakedara = '$uid' ORDER BY id DESC LIMIT 5");
if ($dep && $dep->num_rows > 0) {
    while ($row = $dep->fetch_assoc()) {
        echo "ID:{$row['id']} | Amount:{$row['motta']} | Status:{$row['sthiti']} | Time:{$row['dinankavannuracisi']}\n";
    }
} else {
    echo "কোনো Deposit-History নেই (Reset সফল হয়েছে)\n";
}

echo "\n=== ধাপ ২ — pending_verifications Table ===\n";
$pv = $conn->query("SELECT id, trxid, gateway, status, wants_bonus, expires_at FROM pending_verifications WHERE user_id = '$uid' ORDER BY id DESC LIMIT 5");
if ($pv && $pv->num_rows > 0) {
    while ($row = $pv->fetch_assoc()) {
        echo "ID:{$row['id']} | TrxID:{$row['trxid']} | Gateway:{$row['gateway']} | Status:{$row['status']} | WantsBonus:{$row['wants_bonus']} | Expires:{$row['expires_at']}\n";
    }
} else {
    echo "কোনো Pending-Verification নেই\n";
}

echo "\n=== ধাপ ৩ — first_deposit_bonus_claims Table ===\n";
$bonusCheck = $conn->query("SHOW TABLES LIKE 'first_deposit_bonus_claims'");
if (!$bonusCheck || $bonusCheck->num_rows === 0) {
    echo "⚠️ Table-ই নেই! first_deposit_bonus_schema.sql Run করা হয়নি।\n";
} else {
    $bc = $conn->query("SELECT * FROM first_deposit_bonus_claims WHERE user_id = '$uid' ORDER BY id DESC LIMIT 5");
    if ($bc && $bc->num_rows > 0) {
        while ($row = $bc->fetch_assoc()) {
            echo "ID:{$row['id']} | Deposit:{$row['deposit_amount']} | Bonus:{$row['bonus_amount']} | Status:{$row['status']} | Created:{$row['created_at']}\n";
        }
    } else {
        echo "কোনো Bonus-Claim তৈরি হয়নি\n";
    }
}

echo "\n=== ধাপ ৪ — orphan_sms_log Table (এই User-এর Sender-Mobile দিয়ে) ===\n";
$userMobile = null;
$mQ = $conn->query("SELECT mobile FROM shonu_subjects WHERE id = '$uid'");
if ($mQ && $mQ->num_rows > 0) {
    $userMobile = $mQ->fetch_assoc()['mobile'];
    echo "User-এর নিজের Mobile: $userMobile\n";
}
$orphanCheck = $conn->query("SHOW TABLES LIKE 'orphan_sms_log'");
if ($orphanCheck && $orphanCheck->num_rows > 0) {
    $orphans = $conn->query("SELECT id, trxid, amount, gateway, is_claimed, created_at FROM orphan_sms_log ORDER BY id DESC LIMIT 5");
    if ($orphans && $orphans->num_rows > 0) {
        while ($row = $orphans->fetch_assoc()) {
            echo "ID:{$row['id']} | TrxID:{$row['trxid']} | Amount:{$row['amount']} | Gateway:{$row['gateway']} | Claimed:{$row['is_claimed']} | Time:{$row['created_at']}\n";
        }
    } else {
        echo "কোনো Orphan-SMS নেই\n";
    }
} else {
    echo "orphan_sms_log Table নেই\n";
}

echo "\n=== ধাপ ৫ — Current Balance ===\n";
$bal = $conn->query("SELECT motta FROM shonu_kaichila WHERE balakedara = '$uid'");
if ($bal && $bal->num_rows > 0) {
    echo "Balance: " . $bal->fetch_assoc()['motta'] . "\n";
}
