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

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

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

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


  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") ));

    }


    if(isset($_GET['cht'])) {

      $senderId = $decodeToken->ID;
      $receiverId = mysql_prep(@$_GET['cht']);

      //chats

      $chats = find_all_chats($senderId, $receiverId, 1 );


      $filteredChats = []; 

      foreach ($chats as $chat) {  
        $chatArray = (array) $chat; // Ensure it's an array

        //unset($userArray['tbl_userPass']); // Remove password field

        $filteredChats[] = $chatArray; // Add modified user to new array
      }


       exit(json_encode($filteredChats));


    }







  }else{

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

  }