<?php

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

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

    $fetchTrendingGames = "SELECT * FROM `games` WHERE `priority`= 1 ORDER BY RAND() LIMIT 5";
    $fetchTrendGames = mysqli_query($conn,$fetchTrendingGames);

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

        $fetchAllGames = "SELECT * FROM `games` ORDER BY RAND()";
        $fetchGames = mysqli_query($conn,$fetchAllGames);

        if($fetchGames)
        {

            while($row=$fetchGames->fetch_assoc())
            {
                $response['gamesList'][]=$row;
            }
            $response['statusCode']=0;
            $response['message']='Successful';

            //Fetching adEnabled
            $fetchAdEnabled = "SELECT * FROM `basicsettings` WHERE `id`=2";
            $fetchEnabled = mysqli_query($conn,$fetchAdEnabled);
            if($fetchEnabled)
            {
            while($row=$fetchEnabled->fetch_assoc())
            {
                $response['adEnabled'] = $row['value'];
            }

            //Interstitial Ad Id
            $fetchInterstitialAdId = "SELECT * FROM `adsettings` WHERE `id` = 2";
            $fetchInterstitial = mysqli_query($conn,$fetchInterstitialAdId);

            if($fetchInterstitial)
            {
                while($row=$fetchInterstitial -> fetch_assoc())
                {
                    $response['interstitialAdId'] = $row['adId'];
                }
            }

        }
        else
        {
            $response['statusCode']=4;
            $response['message']='Error in fetching all games'; 
        }
    }


    }
    else
    {
        $response['statusCode']=4;
        $response['message']='Error in fetching trending games'; 
    }

    echo json_encode($response);





?>