<?php
// ini_set('display_errors', 1);
require_once '../../resources/base_path_include.php';
// should we not call header and ch
//valdation file
require_once $base_path_to_learning_resources . "includes/connect.php"; // use require for mandatory files
require_once $base_path_to_learning_resources . 'lib/function.php';

require_once $base_path_to_learning_resources . 'lib/validate_api_signature.php';
require_once $base_path_to_learning_resources.'lib/validate_function.php';

require_once $base_path_to_learning . 'user_management/authenticate.php';

require_once $base_path_to_learning . "vendor/autoload.php";
require_once $base_path_to_learning_resources . 'lib/bucket_functions/bucket_functions.php';
require_once $base_path_to_learning_resources . 'includes/aws_bucket_config.php';

use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;

$postdata = json_decode(file_get_contents("php://input"), true);
checkIfNoPhpContentsReceive();

if (!isset($postdata['institution_id']) or !checkIfIssetAndNotEmpty($postdata['institution_id']) or 
!isset($postdata['api_signature']) or !checkIfIssetAndNotEmpty($postdata['api_signature']) or
!isset($postdata['function_type']) or !checkIfIssetAndNotEmpty($postdata['function_type'])  or
!isset($postdata['jwt_token']) or  !checkIfIssetAndNotEmpty($postdata['jwt_token'])
) {
    errorreturn("All * fields are required.");
}

validateApiSignature($postdata['api_signature']);
$jwttoken = $postdata['jwt_token'];
$jwt_validate = authenticate_jwt($jwttoken);
$userid = $jwt_validate->data->id;
$workspace_id = $jwt_validate->data->workspace_id;
$role = $jwt_validate->data->role;

function getSpecificInstitute($conn_rjllearning, $workspace_id, $institution_id)
{
    $sql = $conn_rjllearning->prepare("SELECT * FROM  institutions  where id = ? and workspace_id =?");
    $sql->execute(array($institution_id,$workspace_id));
    $rows = $sql->fetchAll();
    
    if (count($rows) == 1) {
        // if($rows[0]['logo'] != null ) {
        //      $url_link = 'https://rejoicebilldesk.in/rjllearning_development/';
        //     $aws_attachment = $url_link.$rows[0]['logo'];
        //     $final_array_logo   = $aws_attachment;
        // } else {
        //     $final_array_logo =  null;
        // }
       
        if($rows[0]['logo'] != null and !is_array($rows[0]['logo'])) {
            $data_attachment = json_decode($rows[0]['logo'], true);
            $filePath = $data_attachment['filepath'];
            $originalName = $data_attachment['original_name'];
            $aws_attachment = displayFileFromS3($GLOBALS['s3_learning_bucket'], $GLOBALS['learning_bucketName'], $filePath);
            $final_array_logo   = array("filepath" => $aws_attachment, "original_name" => $originalName);
        } else {
            $final_array_logo =  null;
        }
        
        return array('institution_id' => $rows[0]['id'],'institution_code' => $rows[0]['institution_code'],'institution_name' => $rows[0]['institution_name'],'address' => $rows[0]['address'],'contact_number' => $rows[0]['contact_number']
         ,'email' => $rows[0]['email'] ,'website' => $rows[0]['website'] ,'tagline' => $rows[0]['tagline'] ,'institution_name_abbreviation' => $rows[0]['institution_name_abbreviation'] 
         ,'unique_institution_id' => $rows[0]['unique_institution_id'],'logo' => $final_array_logo 
         ,'white_label_url' => $rows[0]['white_label_url'],'white_label_email' => $rows[0]['white_label_email'],'white_label_message_header' => $rows[0]['white_label_message_header']
         ,'white_label_app_name' => $rows[0]['white_label_app_name']);
      
    }else{
          errorreturn("Institution not found.");
    }
}


if($postdata['function_type']  == "fetch_specific_institutions") { 
  $function_code = $postdata['function_type'];
  $institution_id = $postdata['institution_id'];    

   $success['data'] = getSpecificInstitute($conn_rjllearning,$workspace_id, $institution_id);
  successreturn($success);
  
}

