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

// Validation file
require_once $base_path_to_learning_resources . 'lib/validate_function.php';
require_once $base_path_to_learning . '/user_management/check_username_availability.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 . 'functions/common_functions.php';


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

// Check required fields
if (
    !isset($postdata['institution_id']) || !checkIfIssetAndNotEmpty($postdata['institution_id']) ||
     !isset($postdata['extra_paid_leave_id']) || !checkIfIssetAndNotEmpty($postdata['extra_paid_leave_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 required fields must be provided.");
}

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;

// Validate user access
$specific_db_name = validateUserAccess($conn_rjllearning, $userid, $role, $workspace_id, ['superadmin', 'admin'], null);
if (!$specific_db_name) {
    errorreturn('Access denied.');
}

$conn_rjllearning_specific = dynamicDbConnection($specific_db_name);
require_once $base_path_to_learning . 'functions/get_branch_functions.php';

function getSpecificExtraPaidLeaves($conn_rjllearning_specific, $institution_id, $extra_paid_leave_id)
{   // Base query
    $sql_holiday = "SELECT * FROM extra_paid_leaves WHERE id = ? AND institution_id = ?";
    $array_params = [$extra_paid_leave_id, $institution_id];
    $stmt = $conn_rjllearning_specific->prepare($sql_holiday);
    $stmt->execute($array_params);
    $leaves_rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    if (!empty($leaves_rows)) {
        $row = $leaves_rows[0]; // Get the first row
    
        // Fetch branch info
        // $branch_info = getBranchInfo($conn_rjllearning_specific, $institution_id, $row['branch_id']);
        $branch_info = $row['branch_id'] == 'all' ? array('branch_id' => 'all', 'name' => ucwords('all')) : getBranchInfo($conn_rjllearning_specific, $institution_id, $row['branch_id']);
    
        $array_return = [
            'holiday_id' => $row['id'],
            'branch_id' => $branch_info,
            'holiday_name' => $row['name'],
            'leave_type' => $row['leave_type'],
            'start_date' => $row['start_date'],
            'end_date' => $row['end_date'],
            'remarks' => $row['remarks'],
            'status' => $row['status'],
        ];
    
        return $array_return;
    }else{
        errorreturn("No data found.");
    }

}



if ($postdata['function_type'] == 'fetch_specific_extra_paid_leaves') {
    
    $institution_id = trim($postdata['institution_id']);
    $extra_paid_leave_id = trim($postdata['extra_paid_leave_id']);

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

    $success['data'] = getSpecificExtraPaidLeaves($conn_rjllearning_specific, $institution_id,$extra_paid_leave_id);
    successreturn($success);
    
    
}