<?php

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

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

    //For Quiz History
    $uId=$_POST['uId'];
    $quizName=$_POST['quizName'];
    $icon=$_POST['icon'];
    $color=$_POST['color'];
    $totalQuestions=$_POST['totalQuestions'];
    $questionAttempted=$_POST['questionAttempted'];
    $correctAnswers=$_POST['correctAnswers'];
    $wrongAnswers=$_POST['wrongAnswers'];
    $entryFee=$_POST['entryFee'];
    $prize=$_POST['prize'];

    //For Playing History
    $activity=$_POST['activity'];

    
    //Updating User Coins
    $updateUserCoins = "UPDATE `users` SET `coins`= `coins`+'$prize', `totalCoins`=`totalCoins`+'$prize' WHERE `uId` = '$uId' ";

    $update=mysqli_query($conn,$updateUserCoins);

    if($update)
    {
        //Creating playing history
        $createPlayingHistory = "INSERT INTO `playinghistory` (`id`, `uId`, `icon`, `activity`, `coins`, `paid`, `status`, `type`, `date`) VALUES (NULL, '$uId', '$icon', '$activity', '$prize', '0', '1', 'QUIZ', current_timestamp())";
        $create = mysqli_query($conn,$createPlayingHistory);

        if($create)
        {
            
            //Creating Quiz History
            $createQuizHistory="INSERT INTO `quizhistory` (`id`, `uId`, `quizName`, `icon`, `color`, `totalQuestions`, `questionAttempted`, `correctAnswers`, `wrongAnswers`, `entryFee`, `prize`, `date`) VALUES (NULL, '$uId', '$quizName', '$icon', '$color', '$totalQuestions', '$questionAttempted', '$correctAnswers', '$wrongAnswers', '$entryFee', '$prize', current_timestamp())";

            $createHistory=mysqli_query($conn,$createQuizHistory);

            if($create)
            {
                $response['statusCode'] = 0;
                $response['message'] = 'Successful';  
            }
            else
            {
                $response['statusCode'] = 4;
                $response['message'] = 'Error occurred in creating quiz history';  
            }

        }
        else
        {
            $response['statusCode'] = 4;
            $response['message'] = 'Error occurred in creating playing history';  
        }
    }
    else
    {
        $response['statusCode'] = 4;
        $response['message'] = 'Error occurred in updation user coins';  
    }

    echo json_encode($response);


?>