<?php header("Access-Control-Allow-Origin: *");
  header("Access-Control-Allow-Headers: *");  

  header('Content-Type: application/json');

  $json = file_get_contents('php://input');
  $obj = json_decode($json, true);

  include '../db/connectMe.php';   
  include '../function/function.php';  

  // (C) GENERATE JWT TOKEN
  require "../vendor/autoload.php";
  use Firebase\JWT\JWT;

  // (A) NO data
  if (!isset($obj["purchaseToken"])   ) {

    $error = array("error"=> "no");
    exit( json_encode($error) ); 

  }


  if( AuthorizeMe(@substr(getallheaders()["Authorization"], 7)) ){

    $jwt = substr(getallheaders()["Authorization"], 7);

    //Decoding data
    $decodeToken = (json_decode(base64_decode(str_replace('_', '/', str_replace('-','+',explode('.', $jwt)[1]))))->data);

    if(!isset($decodeToken)){

      exit(json_encode(array("error" => "unauthorised") ));

    }



    //delete purchase

    if( isset($obj['purchaseToken']) )  {

      @$removeInventId =  mysql_prep($obj['purchaseToken']);
      @$status= 0;

      $transInfo = find_transaction_by_id($removeInventId);
      $prodSet = find_product_by_id($transInfo->tbl_productId);  

      //find product cateory by id
      $proCategInfo = find_prod_categ_by_id($prodSet->tbl_product_categId);
  


      $date = date("Y-m-d"); 
      $stmt = $connectMe->prepare(" UPDATE tbl_inventory SET tbl_inventoryStatus =? WHERE tbl_inventoryId =?  ");  

        $stmt->bindParam(1, $status);
        $stmt->bindParam(2, $removeInventId);

        if($stmt->execute()){
          
            //send notifications
            $allUsers = find_all_users_for_business($proCategInfo->tbl_businessId, 1);

            foreach ($allUsers as $userData) {    

              //find user type by id
              $UserType =  find_user_categ_by_id(mysql_prep($userData->tbl_user_categId));


              //if he/she is admin or director 
              if( (strtolower(mysql_prep($UserType->tbl_user_categName)) =="admin") || (strtolower(mysql_prep($UserType->tbl_user_categName)) =="director")  ){  

            
                $title ="Ibyaranguwe: ".$prodSet->tbl_productBrand." byasibwe";
                NotifyMe(0, $title, $userData->tbl_userId);

              }

            }
            

            if($decodeToken->LANGUAGE == 2){    //KINYARWANDA
      
              $message = array("message"=> "Ibyaranguwe: ".$prodSet->tbl_productBrand." byasibwe");
              exit( json_encode($message) ); 

            }else{  //ENGLISH

              $message = array("message"=> "Purchased: ".$prodSet->tbl_productBrand." have been deleted");
              exit( json_encode($message) ); 


            }




      
        }else{

          if($decodeToken->LANGUAGE == 2){    //KINYARWANDA

            $error = array("error"=> 'Igikorwa ntabwo kibashije kurangira, ongera ugerageze');
            exit( json_encode($error) ); 

          }else{


            $error = array("error"=> 'Something went wrong, please try again');
            exit( json_encode($error) ); 

          }
        
        }



    }


















  }else{


    exit(json_encode(array("error" => "unauthorised") ));

  }