<?php

// Set up error reporting
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_resources . 'includes/specific_db_config.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';

require_once $base_path_to_learning  . 'admin_api/modules/library_management/library_function.php';


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


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

// $postdata = $_POST;

$required_fields = ['institution_id', 'branch_id','book_issue_id', 'api_signature', 'function_type', 'jwt_token'];
foreach ($required_fields as $field) {
    if (!isset($postdata[$field]) || !checkIfIssetAndNotEmpty($postdata[$field])) {
        errorreturn("Missing required field: $field");
    }
}


validateApiSignature($postdata['api_signature']);

// Authenticate user and get user details
$jwttoken = $postdata['jwt_token'];
$jwt_validate = authenticate_jwt($jwttoken);
$userid = $jwt_validate->data->id;
$workspace_id = $jwt_validate->data->workspace_id;
$username = $jwt_validate->data->username;
$role = $jwt_validate->data->role;

// Validate user access to specific database
$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);
}
// Include necessary function files
require_once $base_path_to_learning . 'functions/common_functions.php';
require_once $base_path_to_learning . 'functions/get_branch_functions.php';

$institution_id = trim($postdata['institution_id']);
$branch_id = trim($postdata['branch_id']);
$date_of_return= date('Y-m-d');
$return_time=time();
// $fine_amount=($postdata['fine_amount']);
$fine_amount = isset($postdata['fine_amount']) ? trim($postdata['fine_amount']) : null;

$remarks = isset($postdata['remarks']) ? trim($postdata['remarks']) : null;

$academic_id = CheckAcademicSession($conn_rjllearning_specific, $institution_id);
if ($academic_id == false) {
    errorreturn("Access denied. Academic session not active.");
}

if ($postdata['function_type'] == 'return_book') {
    
    $book_issue_id=($postdata['book_issue_id']);
    $result = returnBook($conn_rjllearning_specific, $institution_id, $branch_id, $book_issue_id, $date_of_return, $return_time, $username, $fine_amount, $remarks);

    
        successreturn([$result['message']]);

}
