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

// Include required files
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';
require_once $base_path_to_learning_resources .'lib/common_modules/student_leaves/function.php';
$postdata = json_decode(file_get_contents("php://input"), true);

checkIfNoPhpContentsReceive();

if (
     !isset($postdata['student_id']) || !checkIfIssetAndNotEmpty($postdata['student_id']) ||
    !isset($postdata['api_signature']) || !checkIfIssetAndNotEmpty($postdata['api_signature']) ||
    !isset($postdata['function_type']) || !checkIfIssetAndNotEmpty($postdata['function_type']) ||
    !isset($postdata['jwt_token']) || !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;
$username = $jwt_validate->data->username;
$role = $jwt_validate->data->role;
$institution_id = $jwt_validate->data->institution_id;
$branch_id = $jwt_validate->data->branch_id;

$specific_db_name = validateUserAccess($conn_rjllearning, $userid, $role, $workspace_id, ['principal'], null);

if (!$specific_db_name) {
    errorreturn('Access denied.');
} else {
    $conn_rjllearning_specific = dynamicDbConnection($specific_db_name);
}

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

$version = isset($_POST['version']) ? $_POST['version'] : (isset($postdata['version']) ? $postdata['version'] : null);
$updated_time = isset($_POST['updated_time']) ? $_POST['updated_time'] : (isset($postdata['updated_time']) ? $postdata['updated_time'] : null);

$response = user_data_refresh($conn_rjllearning, $conn_rjllearning_specific,$version, $updated_time, $userid, $institution_id);

if ($postdata['function_type'] == "fetch_specific_student_leaves") {
    
    $student_id=trim($postdata['student_id']);
    $check_academic_confirmation = CheckAcademicSession($conn_rjllearning_specific, $institution_id);
    if ($check_academic_confirmation != true) {
        errorreturn("Access denied. Academic session not active.");
    }
    $limit = $postdata['limit'];
    $page = (isset($postdata['page']) and isNumeric($postdata['page'])) ? (int)$postdata['page'] : 1;

    // $start_date = isset($postdata['start_date']) ? trim($postdata['start_date']) : null;
    if (isset($postdata['start_date']) and !empty($postdata['start_date'])) {
        $start_date = trim($postdata['start_date']);
    } else {
        $start_date = null;
    }
    // $end_date = isset($postdata['end_date']) ? trim($postdata['end_date']) : null;

    if (isset($postdata['end_date']) and !empty($postdata['end_date'])) {
        $end_date = trim($postdata['end_date']);
    } else {
        $end_date = null;
    }

    // Ensure start_date is before end_date
    if ($start_date && $end_date && $start_date > $end_date) {
        errorreturn("The start date cannot be later than the end date.");
    }

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

    // Validate dates
    if ($start_date != null && $end_date != null && strtotime($start_date) > strtotime($end_date)) {
        errorreturn('Invalid Date format');
    }

    // Call the function to get apply leaves
    if ($limit != 'all') {
        $offset = ($page - 1) * $limit;

        $apply_leaves_data = getapplyleaves($conn_rjllearning_specific, $institution_id,$branch_id,$student_id,$start_date, $end_date, $limit, $offset);
    } else {
        $apply_leaves_data = getapplyleaves($conn_rjllearning_specific, $institution_id,$branch_id,$student_id, $start_date, $end_date,  $limit, null);
    }
    successreturn($apply_leaves_data);
}
