<?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 '../programList/programList.php';
include_once '../config/bukku.php';
include_once '../programList/bukkuPGroup.php';
  
// instantiate database and product object
$database = new Database();
$db = $database->getConnection();
$bukku = new Bukku();
$bBT = $bukku->getToken();
$bUrl = $bukku->getBaseURL();
  
// initialize object
$programList = new ProgramList($db);
$bukkuPGroup = new BukkuPGroup($bBT, $bUrl, $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
            $programList->coachName = $data->coachName;
            
            $result = $programList->coachGetProgramList();
            
            if (!empty($result)){
                http_response_code(200);
                echo json_encode($result);
            }else{
                http_response_code(404);
                echo json_encode(array("message" => "Empty Result"));
            }
        
        }else{
            // set response code - 400 bad request
            http_response_code(400);
            // tell the user
            echo json_encode(array("message" => "Unable to get Program List. Input data is empty"));
        }
        break;
        
    case 'PUT':
        
        // make sure data is not empty
        if(!empty($data)){
            // set product property values
            $programList->programData = $data;
            $bukkuPGroup->programName = $data->programName;
            
            $resultBukkuPGroup = $bukkuPGroup->createProductGroup();
            
            if($resultBukkuPGroup != false){
                $programList->bukkuGroupID = $resultBukkuPGroup;
                $programID = $programList->coachUpdateProgramList();
            }
            
            if(!empty($programID)){
                
                $resultTrigger = $programList->createTrigger($data->programName, $programID, $data->programCategory);
                
                if($resultTrigger == true){
                    http_response_code(200);
                    echo json_encode(array("message" => "The custom program added succesfully"));
                }else{
                    http_response_code(404);
                    echo json_encode(array("message" => $resultTrigger));
                }
            }else{
                http_response_code(404);
                echo json_encode(array("message" => "Unable to add custom program. Group ID creation failed"));
            }
        
        }else{
            // set response code - 400 bad request
            http_response_code(400);
            // tell the user
            echo json_encode(array("message" => "Unable to get Program List. Input data is empty"));
        }
        break;
        
    case 'PATCH':
        
        // make sure data is not empty
        if(!empty($data)){
            // set product property values
            $programList->programData = $data;
            
            $result = $programList->coachEnrollProgram();
            
            if ($result){
                http_response_code(200);
                echo json_encode(array("message" => "Program(s) Enrollment Successful"));
            }else{
                http_response_code(404);
                echo json_encode(array("message" => "Program(s) Enrollment Failed"));
            }
        
        }else{
            // set response code - 400 bad request
            http_response_code(400);
            // tell the user
            echo json_encode(array("message" => "Unable to Enroll Program(s). Input data is empty"));
        }
        break;
        
        default:
            // set response code - 405 bad request
            http_response_code(405);
            // tell the user
            echo json_encode(array("message" => "Invalid Method"));
            break;
}