<?php 
include "../../conn.php";
include "../../functions2.php";

header('Content-Type: application/json; charset=utf-8');
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization');
header('Access-Control-Allow-Credentials: true');

$origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '*';
header('Access-Control-Allow-Origin: ' . $origin);

date_default_timezone_set("Asia/Kolkata");
$time = date("Y-m-d H:i:s");

$response = [
    "code"=>11,
    "msg"=>"Method not allowed",
    "msgCode"=>12,
    "serviceNowTime"=>$time
];

if($_SERVER['REQUEST_METHOD']=="POST"){

    $body = file_get_contents("php://input");
    $post = json_decode($body,true);

    if(isset($post['language'],$post['taskId'],$post['random'],$post['signature'],$post['timestamp'])){

        $taskId = intval($post['taskId']);
        $random = mysqli_real_escape_string($conn,$post['random']);
        $language = intval($post['language']);
        $signature = $post['signature'];

        $str='{"language":'.$language.',"random":"'.$random.'","taskId":'.$taskId.'}';
        $checkSign=strtoupper(md5($str));

        if($checkSign==$signature){

            if(!isset($_SERVER['HTTP_AUTHORIZATION'])){
                echo json_encode(["code"=>401,"msg"=>"Unauthorized"]);
                exit;
            }

            $bearer = explode(" ", $_SERVER['HTTP_AUTHORIZATION']);
            $token = $bearer[1];

            $jwt = json_decode(is_jwt_valid($token),true);

            if($jwt['status']=="Success"){

                $userId=$jwt['payload']['id'];

                $check=mysqli_query($conn,"SELECT id FROM egrahcer_sonub 
                WHERE dr='$userId' AND sturgis='$taskId'");

                if(mysqli_num_rows($check)>0){
                    echo json_encode([
                        "code"=>2,
                        "msg"=>"Task already claimed"
                    ]);
                    exit;
                }

                $reward=[
                    1=>25,
                    2=>5,
                    3=>50,
                    4=>250,
                    5=>500,
                    6=>1500,
                    7=>2500,
                    8=>5000
                ];

                if(!isset($reward[$taskId])){
                    echo json_encode([
                        "code"=>7,
                        "msg"=>"Invalid task"
                    ]);
                    exit;
                }

                $amount=$reward[$taskId];

                mysqli_query($conn,"INSERT INTO egrahcer_sonub (dr,sturgis,status,time)
                VALUES('$userId','$taskId','1','$time')");

                mysqli_query($conn,"UPDATE shonu_kaichila 
                SET motta=motta+$amount 
                WHERE balakedara='$userId'");

                $response['code']=0;
                $response['msg']="Succeed";
                $response['msgCode']=0;

                echo json_encode($response);

            }
        }
        else{
            echo json_encode([
                "code"=>5,
                "msg"=>"Wrong signature"
            ]);
        }

    }
    else{
        echo json_encode([
            "code"=>7,
            "msg"=>"Param Invalid"
        ]);
    }

}
else{
    http_response_code(405);
    echo json_encode($response);
}
?>