<?php

    header("Access-Control-Allow-Origin: *");
    header("Content-Type: application/json");
    header("Accept: application/json");

    require '../config/conn.php';

    $getTopRankers = "SELECT `name`, `avatar`, `coins` FROM `users` ORDER BY `coins` DESC LIMIT 3";
    $fetchTopRanks = mysqli_query($conn,$getTopRankers);

    if($fetchTopRanks)
    {
        while($row=$fetchTopRanks->fetch_assoc())
        {
            $response['topRankers'][]= $row;
        }

        $fetchLeaderboardData = "SELECT `name`, `avatar`, `coins` FROM `users` ORDER BY `coins` DESC";
        $getLeaderboard = mysqli_query($conn,$fetchLeaderboardData);

        if($getLeaderboard)
        {
            $response['statusCode'] =0;
            $response['message']= "Successful";
            
            while($row=$getLeaderboard ->fetch_assoc())
            {
                $response['leaderboardList'][]= $row;
            }
        }
        else
        {
            $response['statusCode'] =4;
            $response['message']= "Error in fetching leaderboard data"; 
        }

    }
    else
    {
        $response['statusCode'] =4;
        $response['message']= "Error in fetching top rankers";
    }

    echo json_encode($response);


?>