<?php
// required headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
  
// database connection will be here
// include database and object files
include_once '../config/database.php';
include_once '../requestedSessions/requestedSessions.php';
  
// instantiate database and product object
$database = new Database();
$db = $database->getConnection();
  
// initialize object
$sessions = new RequestSessions($db);

$request_method=$_SERVER["REQUEST_METHOD"];

// get posted data
$data = json_decode(file_get_contents("php://input"));

switch($request_method)
{
    case 'POST':
        // make sure data is not empty
        if(!empty($data)){
            // set product property values
            $sessions->newSession = json_encode($data);
        
            $result = $sessions->createRequestSessions();
            
            if ($result){
                http_response_code(200);
                $response=array(
                    'status' => 1,
                    'status_message' =>'Request Session Added Successfully.');
                    echo json_encode($response);
            }else{
                http_response_code(404);
                $response=array(
                    'status' => 0,
                    'status_message' =>'Session Request Addition Failed.');
                    echo json_encode($response);
            }
        }else{
            // set response code - 400 bad request
            http_response_code(400);
            // tell the user
            echo json_encode(array("message" => "Empty Data"));
        }
        
        break;
        
    case 'PATCH':
        // make sure data is not empty
        if(!empty($data)){
            // set product property values
            $sessions->newSession = json_encode($data);
        
            $result = $sessions->updateRequestSessions();
            
            if ($result){
                http_response_code(200);
                $response=array(
                    'status' => 1,
                    'status_message' =>'Request Session Updated Successfully.');
                    echo json_encode($response);
            }else if($result == 'error'){
                http_response_code(404);
                $response=array(
                    'status' => 0,
                    'status_message' => $result);
                    echo json_encode($response);
            }
            else{
                http_response_code(404);
                $response=array(
                    'status' => 0,
                    'status_message' =>'Session Request Update Failed.');
                    echo json_encode($response);
            }
        }else{
            // set response code - 400 bad request
            http_response_code(400);
            // tell the user
            echo json_encode(array("message" => "Empty Data"));
        }
        break;
        
    default:
            // set response code - 405 bad request
            http_response_code(405);
            // tell the user
            echo json_encode(array("message" => "Invalid Method"));
            break;
}
