<?php

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

    require '../config/conn.php';
    
    $questionLimit;
    $currentDate = date('Y-m-d');


    $fetchDailyQuizEntryFee="SELECT * FROM `dailyquizsettings` WHERE `id`= 1 ";
    $fetchEntryFee = mysqli_query($conn,$fetchDailyQuizEntryFee);

    $fetchDailyQuizPrizePerQuestion = "SELECT * FROM `dailyquizsettings` WHERE `id` = 2";
    $fetchPrize = mysqli_query($conn,$fetchDailyQuizPrizePerQuestion);

    $fetchDailyQuizQuestionLimit = "SELECT * FROM `dailyquizsettings` WHERE `id` = 3";
    $fetchLimit = mysqli_query($conn,$fetchDailyQuizQuestionLimit);

    $fetchDailyQuizLivesAvailable = "SELECT * FROM `dailyquizsettings` WHERE `id` = 4";
    $fetchLives = mysqli_query($conn,$fetchDailyQuizLivesAvailable);

    $fetchDailyQuizTime = "SELECT * FROM `dailyquizsettings` WHERE `id` = 5";
    $fetchTime = mysqli_query($conn,$fetchDailyQuizTime);


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

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

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

                if($fetchLives)
                {
                    while($row=$fetchLives->fetch_assoc())
                    {
                        $response['livesAvailable']=$row['value'];
                    }
                    
                    if($fetchTime)
                    {

                        while($row=$fetchTime->fetch_assoc())
                        {
                            $response['time']=$row['value'];
                        }

                        $fetchDailyQuizQuestions = "SELECT * FROM `dailyquizquestions` WHERE DATE(questionDate) = '$currentDate' ORDER BY RAND() LIMIT $questionLimit";
                        $fetchQuestions=mysqli_query($conn,$fetchDailyQuizQuestions);
        
                        if($fetchQuestions)
                        {
                            while($row=$fetchQuestions->fetch_assoc())
                            {
                                $response['dailyQuizQuestions'][]=$row;
                            }
        
                            $response['statusCode']= 0;
                            $response['message'] = "Sucessful";
                        }
                        else
                        {
                            $response['statusCode'] = 4;
                            $response['message'] = 'Error in fetching daily quiz question';
                        }
                    }
                    else
                    {
                        $response['statusCode'] = 4;
                        $response['message'] = 'Error in fetching quiz time';
                    }

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


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


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

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


    echo json_encode($response);


?>