<?php
require_once __DIR__ . '/../../../../function/connect.php';
header('Content-Type: application/json');

$bonusMap = [
    1 => 100000,
    2 => 75000,
    3 => 65000,
    4 => 57500,
    5 => 52500,
    6 => 47500,
    7 => 45000,
    8 => 42500,
    9 => 40000,
    10 => 37500,
];

$q = "
    SELECT
        t.id_user,
        u.username,
        u.avatar,
        SUM(t.nominal) AS total_win
    FROM tb_bonus t
    JOIN tb_user u ON u.extplayer = t.id_user
    WHERE t.id_bonus = '7'
    GROUP BY t.id_user, u.username, u.avatar
    ORDER BY total_win DESC
    LIMIT 100
";

$result = mysqli_query($koneksi, $q);

$data = [];
$rank = 1;

while ($row = mysqli_fetch_assoc($result)) {
    $bonus = $bonusMap[$rank] ?? 0;

    $data[] = [
        'rank'      => $rank,
        'username'  => substr($row['username'],0,2).'****'.substr($row['username'],-1),
        'avatar'    => $row['avatar'] ?: 'default.png',
        'total_win' => (int)$row['total_win'],
        'bonus'     => (int)$bonus
    ];

    $rank++;
}

echo json_encode($data);