<?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';
require_once $base_path_to_learning_resources . 'lib/task_schedule/task_scheduler_functions.php';


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


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

// $postdata = $_POST;


// print_r($postdata);die();
$required_fields = ['institution_id', 'branch_id','book_id','student_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';


$currenttime = time();

$institution_id = trim($postdata['institution_id']);
$branch_id = trim($postdata['branch_id']);
$student_id = trim($postdata['student_id']);
$book_id = trim($postdata['book_id']);

$date_of_issue = isset($postdata['date_of_issue']) ? $postdata['date_of_issue'] : date('Y-m-d');
$issue_time =time() ;

// $issue_time = time();
$due_date = trim($postdata['due_date']);
$date_of_return = isset($postdata['date_of_return']) ? trim($postdata['date_of_return']) : null;
$return_time = isset($postdata['return_time']) ? trim($postdata['return_time']) : null;
// $fine_amount = trim($postdata['fine_amount']);
$fine_amount = isset($postdata['fine_amount']) ? trim($postdata['fine_amount']) : null;
$remarks = isset($postdata['remarks']) ? trim($postdata['remarks']) : null;
// $remarks = trim($postdata['remarks']);
$date_created = date('Y-m-d');
$time_created = time();




$academic_id = CheckAcademicSession($conn_rjllearning_specific, $institution_id);
if ($academic_id == false) {
    errorreturn("Access denied. Academic session not active.");
}
  compareDatesAcademicSession($conn_rjllearning_specific, $institution_id, $due_date, null, true);
if ($postdata['function_type'] == 'book_issue') {
    $result = issueBook(
    $conn_rjllearning_specific,
    $conn_rjllearning,
    $workspace_id,
    $institution_id,
    $branch_id,
    $student_id,
    $book_id,
    $date_of_issue,
    $issue_time,
    $due_date,
    $date_of_return,
    $return_time,
    $username,
    $fine_amount,
    $remarks,
    $date_created,
    $time_created,
    $academic_id
);
        successreturn([$result['message']]);

}
 else if ($postdata['function_type'] == 'alter_book_issue') {

    
    $book_issue_id = $postdata['book_issue_id'];
    updateBookIssue(
    $conn_rjllearning_specific,
    $conn_rjllearning,
    $workspace_id,
    $institution_id,
    $book_issue_id,
    $date_of_issue,
    $issue_time,
    $due_date,
    $date_of_return,
    $return_time,
    $fine_amount,
    $remarks,
    $username,
    $date_created,
    $time_created
);

    successreturn("Library record updated successfully.");
}