<?php
ini_set("display_errors", 1);
	header("Acces-Control-Allow-Origin: *");
	header("Content-type: application/json; charset=UTF-8");
	header("Acces-Control-Allow-Method: POST");
require_once("../Database/Database.php");
	require_once("../Classes/NUMBER.php");
	require_once("../Constants/Constants_Variable.php");
	include_once ("../JWT/autoload.php");
	include_once ("../JWT/JWT.php");
	include_once ("../JWT/BeforeValidException.php");
	include_once ("../JWT/ExpiredException.php");
	include_once ("../JWT/SignatureInvalidException.php");
		use \Firebase\JWT\JWT;
$db=new Database();
	$conn=$db->Connect();
$student=new Users($conn);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}


require __DIR__ . '/vendor/autoload.php';

use Twilio\Rest\Client;
$response = array();

	$data=json_decode(file_get_contents("php://input"));
		if(!empty($data->number))
		{
		    
		$number=$data->number;
 
 try
			{
 	$headers=getallheaders();
				if(array_key_exists('Authorization', $headers) && $headers['Authorization'] != '')
				{
					$token=$headers["Authorization"];
					$secret_key="waheedkhan786";
					$decoded_data=JWT::decode($token,$secret_key,array('HS256'));
					$student->user_id=$decoded_data->data->user_id;
					$student->user_name=$decoded_data->data->user_name;
					$student->user_email=$decoded_data->data->user_email;
					$student->fcm_token=$decoded_data->data->fcm_token;
				}
				else
				{
					throwError(ACCESS_TOKEN_ERROR,"Please add headers");
				}




$sid    = "AC0bd610910620e86c79b0fd36e0a7a677";
$token  = "aa8e63b8357782efab1492f0b2e61799";
$twilio = new Client($sid, $token);

$calls = $twilio->calls
                ->read(["status" => "completed", "to" => $number], 100);


$callsidarray = array();
foreach ($calls as $record) {
 
  $recordings = $twilio->recordings
                     ->read(["callSid" => $record->sid],
                            20
                     );

                     
     foreach ($recordings as $record1) {
          
      
       $twilio->recordings($record1->sid)
       ->delete();
                              }
   
   
    
}


$callsidarray["deleted"] = "yes";

echo json_encode($callsidarray);
}
			catch(DomainException $ex)
			{
				throwError(TOKEN_UPDATE_ERROR,$ex->getMessage());
			}
			catch(UnexpectedValueException $ex)
			{
				throwError(UN_SUCCESS_RESPONSE,$ex->getMessage());
			}
			catch (InvalidArgumentException $e) 
			{
				throwError(ACCESS_TOKEN_ERROR,$e->getMessage());
			}
}else
		{
			throwError(UN_SUCCESS_RESPONSE,"All parameters are required.");
		}
  
    function throwError($code,$message)
		{
			header("content-type:application/json");
			$errorMessage=json_encode(['error'=>['status'=>$code,'message'=>$message]]);
			echo $errorMessage;
			exit;
		}
	 function returnResponse($code,$data)
		{
			header("content-type:application/json");
			$errorMessage=json_encode(['response'=>['status'=>$code,'result'=>$data]]);
			echo $errorMessage;
			exit;
		}


?>