<?php

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

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

    $uId=$_POST['uId'];
    $homeQuizLimit;
    $userTotalCoins;
    $userLevelId;
    $levelId;
    $time = time();

    $getUserData = "SELECT * FROM `users` WHERE `uId`='$uId'";
    $fetchUserData=mysqli_query($conn,$getUserData);

    if($fetchUserData)
    {
        if(mysqli_num_rows($fetchUserData)>0)
        {
            while($row=$fetchUserData->fetch_assoc())
            {
                $response['id']=$row['id'];
                $response['name']=$row['name'];
                $response['avatar']=$row['avatar'];
                $response['coins']=$row['coins'];
                $response['totalCoins']=$row['totalCoins'];
                $response['isBanned']=$row['isBanned'];
                $response['levelId']=$row['levelId'];
                $response['quizPlayed']=$row['quizPlayed'];
                $response['inviteCode']=$row['inviteCode'];
                $response['invitedBy']=$row['invitedBy'];
                $response['accountCreated']=$row['accountCreated'];
                $userTotalCoins = $row['totalCoins'];
                $userLevelId = $row['levelId'];
                $response['time']=$time;

                $getUserLevel="SELECT * FROM `earninglevels` WHERE `maxLimit`>='$userTotalCoins' AND `minLimit`<='$userTotalCoins'";
                $getLevel=mysqli_query($conn,$getUserLevel);

                if($getLevel)
                {
                    if(mysqli_num_rows($getLevel)>0)
                    {
                        while($row=$getLevel->fetch_assoc())
                        {
                            $response['levelName']=$row['name'];
                            $response['levelIcon']=$row['icon'];
                            $response['levelGreetings']=$row['greetings'];
                            $levelId = $row['id'];
                        
                            if($userLevelId != $row['id'])
                            {
                                $response['isLevelChanged']=1;
                                $updateUserLevel="UPDATE `users` SET `levelId`='$levelId' WHERE `uId`='$uId'";
                                $update = mysqli_query($conn,$updateUserLevel);
                            }
                            else
                            {
                                $response['isLevelChanged']=0;
                            }
                        }
                    }
                }

            }
            
            //Fetching home quiz
            $getHomeQuizLimit="SELECT * FROM `basicsettings` WHERE `id`='1'";
            $fetchHomeQuizLimit=mysqli_query($conn,$getHomeQuizLimit);

            if($fetchHomeQuizLimit)
            {
                while($row=$fetchHomeQuizLimit->fetch_assoc())
                {
                    $homeQuizLimit=$row['value'];
                }

                $getHomeQuizzes="SELECT * FROM `quizzes` WHERE 1 ORDER BY `priority` DESC, `id` LIMIT $homeQuizLimit";
                $fetchHomeQuizzes=mysqli_query($conn,$getHomeQuizzes);

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

            }

            //Fetching Categories
            $getCategories="SELECT * FROM `categories` WHERE 1 ORDER BY `priority` DESC";
            $fetchCategories=mysqli_query($conn,$getCategories);

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

            $response['statusCode']=0;
            $response['message']="Data fetched successfully";

        }
        else
        {
            $response['statusCode']=2;
            $response['message']="No user found";
        }

        //Fetching watch earn reward
        $fetchWatchEarnReward="SELECT * FROM `rewardsettings` WHERE `id`=1";
        $fetchReward=mysqli_query($conn,$fetchWatchEarnReward);

        if($fetchReward)
        {
            while($row=$fetchReward->fetch_assoc())
            {
            $response['watchEarnReward']=$row['value'];
            }
        }

        //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'];
            }

            //Fetching ad ids

            //Native ad
            $fetchNativeAdId = "SELECT * FROM `adsettings` WHERE `id` = 1";
            $fetchNative = mysqli_query($conn,$fetchNativeAdId);

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

            //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'];
                }
            }

            //Rewarded Ad Id
            $fetchRewardedAdId = "SELECT * FROM `adsettings` WHERE `id` = 3";
            $fetchRewarded = mysqli_query($conn,$fetchRewardedAdId);

            if($fetchNative)
            {
                while($row=$fetchRewarded -> fetch_assoc())
                {
                    $response['rewardedAdId'] = $row['adId'];
                }
            }

            //Fetching daily check in reward
            $fetchDailyCheckInReward = "SELECT * FROM `basicsettings` WHERE `id`=3";
            $fetchCheckInReward=mysqli_query($conn,$fetchDailyCheckInReward);

            if($fetchCheckInReward)
            {
                while($row=$fetchCheckInReward->fetch_assoc())
                {
                    $response['checkInReward']=$row['value'];
                }
            }




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

    echo json_encode($response)


?>