<?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->coachID = $data->coachID;
            $sessions->dateTimes = $data->dateTimes;
            
            $result = $sessions->checkCoachAvailability();
            
            if(!empty($result)){
                http_response_code(200);
                echo json_encode($result);
            }else{
                http_response_code(400);
                echo json_encode(array("message" => "No existing session(s) found"));
            }
        }else{
            http_response_code(400);
            echo json_encode(array("message" => "Request Error"));
        }
        
        break;
        
        default:
            http_response_code(405);
            echo json_encode(array("message" => "Invalid Method"));
            break;
}
