<?php

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

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

    $uId= $_POST['uId'];
    $coins= $_POST['coins'];
    $type = $_POST['type'];
    $activity = $_POST['activity'];

    //Updating User Coins
    $updateUserCoins = "UPDATE `users` SET `coins`=`coins`+'$coins', `totalCoins`=`totalCoins`+'$coins' 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', '', '$activity', '$coins', '0', '1', '$type', 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 updating user coins';;
    }

    echo json_encode($response);

?>