<?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 '../config/bukku.php';
include_once '../users/users.php';
include_once '../users/bukkuContacts.php';
  
// instantiate database and product object
$database = new Database();
$db = $database->getConnection();
$bukku = new Bukku();
$bBT = $bukku->getToken();
$bUrl = $bukku->getBaseURL();
  
// initialize object
$users = new Users($db);
$bukkuContact = new BukkuContact($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
            $bukkuContact->input = $data;
            $resultBukkuCreateContact = $bukkuContact->createContact();
            
            $bukkuResult = json_decode($resultBukkuCreateContact);
            
            if($bukkuResult != false){
                $users->accDetails = json_encode($data);
                $users->bukkuID = $bukkuResult->contact->id;
                $users->bukkuContactID = $bukkuResult->contact->contact_persons[0]->id; //accessing first id of contact_persons
                
                echo $bukkuResult->contact->contact_persons->id;
                
                $result = $users->createAcc();
                
                if ($result){
                    http_response_code(200);
                    $response=array(
                        'status' => 1,
                        'status_message' =>'New User Added Successfully.');
                        echo json_encode($response);
                }else{
                    http_response_code(404);
                    $response=array(
                        'status' => 0,
                        'status_message' =>'New User Addition Failed.');
                        echo json_encode($response);
                }
            }else{
                http_response_code(404);
                    $response=array(
                        'status' => 0,
                        'status_message' =>'New User Addition Failed(B).');
                        echo json_encode($response);
            }
        }else{
            // set response code - 400 bad request
            http_response_code(400);
            // tell the user
            echo json_encode(array("message" => "Unable to add new user. User data is empty"));
        }
        break;
        
    case 'PATCH':
        
        // make sure data is not empty
        if(!empty($data)){
            // set product property values
            $bukkuContact->input = $data;
            $resultBukkuCreateContact = $bukkuContact->updateContact();
            
            $bukkuResult = json_decode($resultBukkuCreateContact);
            
            if($bukkuResult != false){
                $users->accDetails = json_encode($data);
                $users->bukkuID = $bukkuResult->contact->id;
                
                // set product property values
                $users->accDetails = json_encode($data);
            
                $result = $users->updateAcc();
            
                if (!empty($result)){
                    http_response_code(200);
                    $response=array(
                        'status' => 1,
                        'status_message' => $result);
                        echo json_encode($response);
                }else{
                    http_response_code(404);
                    $response=array(
                        'status' => 0,
                        'status_message' =>'Update User Details Failed.');
                    echo json_encode($response);
                }
                
            }else{
                http_response_code(404);
                    $response=array(
                        'status' => 0,
                        'status_message' =>'Update User Details Failed(B).');
                        echo json_encode($response);
            }
        }else{
            // set response code - 400 bad request
            http_response_code(400);
            // tell the user
            echo json_encode(array("message" => "Unable to add update user. User 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;
}