<?php
ini_set('display_errors', 1);

require_once '../../../../resources/base_path_include.php';

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';

//valdation file
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.'functions/common_functions.php';
//db access function
require_once $base_path_to_learning_resources . 'includes/specific_db_config.php';
//aws image store
require_once $base_path_to_learning_resources . 'lib/bucket_functions/bucket_functions.php';
require_once $base_path_to_learning . "vendor/autoload.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);
//print_r($postdata);
checkIfNoPhpContentsReceive();


if ( !isset($postdata['vendor_id']) or !checkIfIssetAndNotEmpty($postdata['vendor_id']) or
  !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);
//then workspace_id is iin decodede data of jwt use from there
$userid = $jwt_validate->data->id;
$workspace_id = $jwt_validate->data->workspace_id;
$username = $jwt_validate->data->username;
$role = $jwt_validate->data->role;

$specific_db_name = validateUserAccess($conn_rjllearning, $userid, $role, $workspace_id,['superadmin','admin'],null);
if (!$specific_db_name) {
    errorreturn('Access denied.');
} else {
    $conn_rjllearning_specific = dynamicDbConnection($specific_db_name);
}


function getSpecificVendor($conn_rjllearning_specific,$institution_id,$vendor_id) {
    
    $sql = $conn_rjllearning_specific->prepare("SELECT * FROM  vendor where id = ? and institution_id =?");
    $sql->execute(array($vendor_id,$institution_id));
    $rows = $sql->fetchAll();
   
    if (count($rows) == 1) {
        $branch_info  = getBranchInfo($conn_rjllearning_specific, $institution_id, $rows[0]['branch_id']);
        return array('vendor_id'=> $rows[0]['id'],
        'branch_id'=>$branch_info ,
     
        'vendor_name' => $rows[0]['vendor_name'], 
         'institution_id' => $rows[0]['institution_id'],
        'vendor_service' => $rows[0]['vendor_service'],
         'contact_number' => $rows[0]['contact_number'],
          'email' => $rows[0]['email'],
           'address' => $rows[0]['address'],
            'pincode' => $rows[0]['pincode'],
             'pan' => $rows[0]['pan'],
           'gst' => $rows[0]['gst'],
            'created_by' => $rows[0]['created_by'],
               'date_created'=>$rows[0]['date_created'],
               'time_created' => $rows[0]['time_created'],);
      
    }else{
          errorreturn("Enter Invalid vendor info.");
    }

}

if($postdata['function_type']  == "fetch_specific_vendor") {  
    
    $vendor_id = trim($postdata['vendor_id']);
    $institution_id = trim($postdata['institution_id']);    
    $check_academic_confirmation = CheckAcademicSession($conn_rjllearning_specific,$institution_id);
    if($check_academic_confirmation != true){
         errorreturn("Access denied. Academic session not active.");
    }
    $success['data'] = getSpecificVendor($conn_rjllearning_specific,$institution_id,$vendor_id);
    successreturn($success);
      
}

