<?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/bukkuProduct.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);
$bukkuProduct = new BukkuProduct($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->coachGetCourseList();
            
            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 'PATCH':
        
        // make sure data is not empty
        if(!empty($data)){
            // set product property values
            $programList->programData = $data;
            
            $result = $programList->coachUpdateCourseList();
            
            if ($result == 'Success'){
                http_response_code(200);
                echo json_encode(array("message" => $result));
            }else if($result == 'Error1'){
                http_response_code(404);
                echo json_encode(array("message" => "This course recently got new price list. Kindly wait for 48 hours to change the program cost list."));
            }else{
                http_response_code(404);
                echo json_encode(array("message" => $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':
        
        // Ensure data is not empty
        if (!empty($data)) {
            // Step 1: Set product properties for Bukku API call
            $programList->programData = $data;
 
            $priceType = $data->priceType;
            
            if($data->priceType != 'default'){
                $masterLevel = $data->masterLevel;
                $price = $data->price;
            }
            
            $result = $programList->coachAddNewCourse();
            
            if ($result != false) {
                if($priceType == 'default'){
                    $bukkuProduct->input = $data;
                    $programList->sku = $result;
                    $bukkuProduct->sku = $result;
                    $resultBukkuCreateProduct = $bukkuProduct->createProduct();
                    $bukkuProductResult = json_decode($resultBukkuCreateProduct);
                    
                    if ($bukkuProductResult != false && isset($bukkuProductResult->product->id)) {
                        $bukkuID = $bukkuProductResult->product->id;
                        $priceID = $bukkuProductResult->product->units[0]->id;
                        $trialPriceID = $bukkuProductResult->product->units[1]->id ?? null;
                        
                        $programList->bukkuID = $bukkuID;
                        $programList->priceID = $priceID;
                        $programList->trialPriceID = $trialPriceID;
                        
                        $resultUpdateID = $programList->coachNewCourseUpdateID();
                        
                        if($resultUpdateID){
                            http_response_code(200);
                            echo json_encode(array('message' => 'New Course Added Successfully.'));
                        }else{
                            http_response_code(404);
                            echo json_encode(array('message' => 'New Course ID Update Failed.'));
                        }
                        
                    } else {
                        http_response_code(404);
                        echo json_encode(array('message' => 'Bukku Product Creation Failed.'));
                    }
                } else {
                    $resultAddCustomPrice = $programList->coachAddCustomPriceCourse($result, $price, $masterLevel);
                    
                    if($resultAddCustomPrice){
                        http_response_code(200);
                        echo json_encode(array('message' => 'New Course Added Successfully. Awaiting for Approval'));
                    } else {
                        http_response_code(404);
                        echo json_encode(array('message' => 'New Course Addition Failed : Error Occurred'));
                    }  
                }
            } else {
                http_response_code(404);
                echo json_encode(array('message' => 'New Course Addition Failed.'));
            }
        } else {
            // Set response code - 400 bad request for empty data
            http_response_code(400);
            echo json_encode(array("message" => "Unable to add new course. 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;
}