<?php

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

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

    //For Playing history
    $uId = $_POST['uId'];
    $icon = $_POST['icon'];
    $activity = $_POST['activity'];
    $coins = $_POST['coins'];
    $paid= $_POST['paid'];

    //Updating User Coins
    $updateUserCoins = "UPDATE `users` SET `coins`=`coins`-'$coins', `quizPlayed` = `quizPlayed`+1 WHERE `uId`='$uId'";
    $update=mysqli_query($conn,$updateUserCoins);

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

        if($create)
        {
            $response['statusCode']=0;
            $response['message']="Successful";
        }
        else
        {
            $response['statusCode']=4;
            $response['message']="Error in creating Playing History";
        }
    }
    else
    {
        $response['statusCode']=4;
        $response['message']="Error in creating Deducting Coins";
    }


    echo json_encode($response);


?>