<?php

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

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

    $uId=$_POST['uId'];
    $inviteCode=$_POST['inviteCode'];
    $inviteSenderActivity = $_POST['inviteSenderActivity'];
    $inviteeActivity = $_POST['inviteeActivity'];

    $senderUid;
    $inviteSenderReward;
    $inviteeReward;

    //Fetching invite sender reward
    $fetchInviteSenderReward = "SELECT * FROM `rewardsettings` WHERE `id` = 2";
    //Fetching invitee reward
    $fetchInviteeReward = "SELECT * FROM `rewardsettings` WHERE `id` = 3";

    $fetchSenderReward = mysqli_query($conn,$fetchInviteSenderReward);
    $fetchReceiverReward = mysqli_query($conn,$fetchInviteeReward);

    if($fetchSenderReward && $fetchReceiverReward)
    {

        while($row=$fetchSenderReward->fetch_assoc())
        {
            $inviteSenderReward = $row['value'];
        }

        while($row=$fetchReceiverReward->fetch_assoc())
        {
            $inviteeReward = $row['value'];
        }

    }

    //Fetching invite sender uid
    $fetchSenderData = "SELECT * FROM `users` WHERE `inviteCode` = '$inviteCode'";
    $fetchSender = mysqli_query($conn,$fetchSenderData);
    if($fetchSenderData)
    {
        if(mysqli_num_rows($fetchSender)>0)
        {
            while($row=$fetchSender->fetch_assoc())
            {
                $senderUid = $row['uId'];
            }

            //Updating Sender Data 
            $addCoinsToSender = "UPDATE `users` SET `coins` = `coins`+'$inviteSenderReward',`totalCoins` = `totalCoins`+'$inviteSenderReward' WHERE `inviteCode` = '$inviteCode'";
            $updateSender = mysqli_query($conn,$addCoinsToSender);
            if($updateSender)
            {
                //Creating sender playing history
                $createSenderPlayingHistory = "INSERT INTO `playinghistory` (`id`, `uId`, `icon`, `activity`, `coins`, `paid`, `status`, `type`, `date`) VALUES (NULL, '$senderUid', '', '$inviteSenderActivity', '$inviteSenderReward', '0', '1', 'INVITE JOIN EARN', current_timestamp())";

                $createSenderHistory = mysqli_query($conn,$createSenderPlayingHistory);

                if($createSenderHistory)
                {
                    
                    //Updating receiver Data
                    $addCoinsToReceiver = "UPDATE `users` SET `coins` = `coins`+'$inviteeReward' , `totalCoins` = `totalCoins`+'$inviteeReward', `invitedBy` = '$inviteCode' WHERE `uId` = '$uId'";
                    $updateReceiver = mysqli_query($conn,$addCoinsToReceiver);

                    if($updateReceiver)
                    {
                        //Creating receiver playing history
                        $createReceiverPlayingHistory = "INSERT INTO `playinghistory` (`id`, `uId`, `icon`, `activity`, `coins`, `paid`, `status`, `type`, `date`) VALUES (NULL, '$uId', '', '$inviteeActivity', '$inviteeReward', '0', '1', 'INVITE BONUS', current_timestamp())";

                        $createReceiverHistory = mysqli_query($conn,$createReceiverPlayingHistory);

                        if($createReceiverHistory)
                        {
                            $response['statusCode'] = 0;
                            $response['message'] = 'Successful';
                        }
                        else
                        {
                            $response['statusCode'] = 4;
                            $response['message'] = 'Error occurred in creating receiver playing history';
                        }
                    }
                    else
                    {
                        $response['statusCode'] = 4;
                        $response['message'] = 'Error occurred in updating receiver coins';
                    }


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


        }
        else
        {
            $response['statusCode'] = 1;
            $response['message'] = 'Invalid invite code';
        }
    }
    else
    {
        $response['statusCode'] = 0;
        $response['message'] = 'Error in fetching sender data';
    }


    echo json_encode($response);



?>