<?php
/**
 * get_recent_winners.php
 * game_bet_logs Table থেকে, সাম্প্রতিক আসল Win-গুলো এনে দেয় —
 * Privacy রক্ষার জন্য, Mobile Number আংশিক লুকিয়ে (Mask করে)।
 */
include __DIR__ . "/deepanshu/conn.php";
header('Content-Type: application/json; charset=utf-8');
header('Access-Control-Allow-Origin: *');

$result = $conn->query("
    SELECT s.mobile, g.game_code, g.win_amount, g.created_at
    FROM game_bet_logs g
    INNER JOIN shonu_subjects s ON s.id = g.user_id
    WHERE g.win_amount > g.bet_amount
    ORDER BY g.created_at DESC
    LIMIT 15
");

$winners = [];
while ($row = $result->fetch_assoc()) {
    // ✅ Privacy — Mobile Number-এর মাঝের অংশ ঢেকে দেওয়া (018****475)
    $mobile = $row['mobile'];
    $maskedMobile = strlen($mobile) >= 7
        ? substr($mobile, 0, 4) . '****' . substr($mobile, -3)
        : '***';

    $winners[] = [
        'mobile' => $maskedMobile,
        'game' => $row['game_code'],
        'amount' => number_format($row['win_amount'], 0),
        'time' => $row['created_at']
    ];
}

echo json_encode(['code' => 0, 'winners' => $winners]);
