<?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 '../admin/priceList.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();

// initialize object
$priceList = new PriceList($db);

$request_method=$_SERVER["REQUEST_METHOD"];

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

switch($request_method)
{
    case 'POST':
        
        if($data->requestType == '0'){
            $result = $priceList->viewDefaultListForClient();    //Coach use
        } else if ($data->requestType == '1'){
            $result = $priceList->viewCustomList();    //Admin use
        }
        
        if (empty($result)){
            http_response_code(404);
            echo json_encode(array("message" => "Error occured"));
        }else{
            http_response_code(200);
            echo json_encode($result);
        }
        
        break;
        
    //Admin use
    case 'PATCH':
        // make sure data is not empty
        if(!empty($data)){
            
            $priceList->alphanumeric_id = $data->alphanumeric_id;
            $priceList->tableName = $data->tableName;
            $priceList->username = $data->username;
            $priceList->status = $data->status;
  
            $result = $priceList->updateCustomList();
            
            if(empty($result)){
                http_response_code(404);
                echo json_encode(array("message" => "Updating course custom price list failed : Error Occurred"));
            } else if ($result === true){
                $resultUpdateStatus = $priceList->coachCourseUpdateDeclineStatus();
                
                if($resultUpdateStatus){
                    http_response_code(200);
                    echo json_encode(array('message' => 'Course status set to DECLINE successfully'));
                }else{
                    http_response_code(404);
                    echo json_encode(array('message' => 'Course status set to DECLINE failed'));
                }
            } else {
                
                $bukku = new Bukku();
                $bBT = $bukku->getToken();
                $bUrl = $bukku->getBaseURL();
                
                $bukkuProduct = new BukkuProduct($bBT, $bUrl, $db);
                
                $bukkuProduct->sku = $result[0]['alphanumeric_id'];
                $bukkuProduct->courseName = $result[0]['courseName'];
                $bukkuProduct->price = $result[0]['price'];
                $bukkuProduct->trialPrice = $result[0]['trialPrice'];
                $bukkuProduct->programGroupID = $result[1]['bukkuGroupID'];
                
                $resultBukkuCreateProduct = $bukkuProduct->createCustomPriceProduct();
                $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;

                    $priceList->bukkuID = $bukkuID;
                    $priceList->priceID = $priceID;
                    $priceList->trialPriceID = $trialPriceID;
                    
                    $resultUpdateID = $priceList->coachNewCourseUpdateID();
                    
                    if($resultUpdateID){
                        http_response_code(200);
                        echo json_encode(array('message' => 'Custom Course Added Successfully.'));
                    }else{
                        http_response_code(404);
                        echo json_encode(array('message' => 'Custom Course Update Failed.'));
                    }       
                } else {
                    http_response_code(404);
                    echo json_encode(array('message' => 'Bukku Product Creation for Custom Course Failed.'));
                }
            }
        } else {
            // set response code - 400 bad request
            http_response_code(400);
            // tell the user
            echo json_encode(array("message" => "Input data is missing"));
        }
        break;
    
    //Coach use
    case 'PUT':
        // make sure data is not empty
        if(!empty($data)){
            $priceList->intermediatePrice = $data->intermediatePrice;
            $priceList->advancePrice = $data->advancePrice;
            $priceList->masterPrice = $data->masterPrice;
            $priceList->alphanumeric_id = $data->alphanumeric_id;
            
            $result = $priceList->addCustomList();
            
            if ($result){
                http_response_code(200);
                echo json_encode(array("message" => "Sucessfully added course custom price list"));
            }else{
                http_response_code(404);
                echo json_encode(array("message" => "Updating course custom price list failed"));
            }
            
            
        }else{
            // set response code - 400 bad request
            http_response_code(400);
            // tell the user
            echo json_encode(array("message" => "Input data is missing"));
        }
        break;

        default:
            // set response code - 405 bad request
            http_response_code(405);
            // tell the user
            echo json_encode(array("message" => "Invalid Method"));
            break;
}