<?php

ini_set('display_errors', 1);

require_once '../../../resources/base_path_include.php';
require_once $base_path_to_learning_resources  . "includes/connect.php";
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 . 'functions/common_functions.php';
require_once $base_path_to_learning_resources . 'includes/specific_db_config.php';
require_once $base_path_to_learning . 'functions/get_branch_functions.php';
require_once $base_path_to_learning . 'admin_api/modules/gate_pass/functions.php';

// Decode JSON input data
$postdata = json_decode(file_get_contents("php://input"), true);

// Check if no data was received
checkIfNoPhpContentsReceive();

// Validate required fields
$requiredFields = ['institution_id', 'branch_id', 'id', 'api_signature', 'function_type', 'jwt_token'];
foreach ($requiredFields as $field) {
    if (!isset($postdata[$field]) || !checkIfIssetAndNotEmpty($postdata[$field])) {
        errorreturn("All * fields are required.");
    }
}

validateApiSignature($postdata['api_signature']);

// Validate JWT token
$jwttoken = $postdata['jwt_token'];
$jwt_validate = authenticate_jwt($jwttoken);

// Extract user details from JWT
$userid = $jwt_validate->data->id;
$workspace_id = $jwt_validate->data->workspace_id;
$username = $jwt_validate->data->username;
$role = $jwt_validate->data->role;

$institution_id = trim($postdata['institution_id']);
$branch_id = trim($postdata['branch_id']);
$request_id = $postdata['id'];

// var_dump($branch_id,$request_id);

// Validate user access and get specific database name
$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);
}

$isBranchClassSubjectValid = checkBranchClassSubject($conn_rjllearning_specific, $branch_id, null, null, $institution_id);
if (!$isBranchClassSubjectValid) {
    errorreturn("Invalid branch_id.");
}

if($postdata['function_type'] == "approve_gate_pass_request"){
    $institution_id = trim($postdata['institution_id']);
    $branch_id = trim($postdata['branch_id']);
    $request_id = trim($postdata['id']);
    $status = $postdata['status'];

    $check_academic_confirmation_id = CheckAcademicSession($conn_rjllearning_specific, $institution_id);
    if ($check_academic_confirmation_id != true) {
        errorreturn("Access denied. Academic session not active.");
    }
    
    $result = approve_gatepass_request($request_id, $conn_rjllearning, $conn_rjllearning_specific, $username, $institution_id, $branch_id, $status);
    
    successreturn($result);
}else{
    errorreturn("Invalid Function Type");
}