<?php

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

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

    $uId=$_POST['uId'];
    $name=$_POST['name'];
    $email=$_POST['email'];
    $inviteCode=$_POST['inviteCode'];
    
    //Checking user
    $checkUser = "SELECT * FROM `users` WHERE `uId`='$uId'";
    $check=mysqli_query($conn,$checkUser);

    if(mysqli_num_rows($check)>0)
    {
        $response['statusCode']=1;
        $response['message']='User already exist';
    }
    else
    {
        //Creating new user
        $createUser="INSERT INTO `users` (`id`, `uId`, `name`, `email`, `avatar`, `coins`, `totalCoins`, `isBanned`, `levelId`, `quizPlayed`, `inviteCode`, `invitedBy`, `accountCreated`) VALUES (NULL, '$uId', '$name', '$email', '', '0', '0', '0', '', '0', '$inviteCode', NULL, current_timestamp())";

        $create=mysqli_query($conn,$createUser);
        
        if($create)
        {
            $response['statusCode']=0;
            $response['message']='User created successfully';
        }
        else
        {
            $response['statusCode']=4;
            $response['message']='Error in creating user';
        }
    }

    echo json_encode($response);



?>