<?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";
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 . 'functions/common_functions.php';
require_once $base_path_to_learning . 'functions/get_branch_functions.php';
require_once $base_path_to_learning_resources . 'lib/common_modules/attendance/unmarked_report/get_unmarked_attendance_daywise_class_student.php';

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

// print_r($postdata);
// die();
// Check if required fields are present
$required_fields = ['institution_id', 'api_signature', 'function_type', 'jwt_token'];
foreach ($required_fields as $field) {
    if (!isset($postdata[$field]) || !checkIfIssetAndNotEmpty($postdata[$field])) {
        errorreturn("All * fields are required.$field");
    }
}

// Validate API signature
validateApiSignature($postdata['api_signature']);

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

// Extract data from JWT token
$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';




if ($postdata['function_type'] === "fetch_unmarked_attendance_daywise_class_student") {

    $institution_id = trim($postdata['institution_id']);
    if (isset($postdata['branch_id']) and !empty($postdata['branch_id']) and trim($postdata['branch_id'])) {
        $branch_id = trim($postdata['branch_id']);
    } else {
        $branch_id = "all";
    }
   $date = !empty($postdata['date']) ? $postdata['date'] : date('Y-m-d');


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

    $data = getUnmarkedAttendanceDaywiseClassStudent($conn_rjllearning_specific, $institution_id, $branch_id, $date);
    $success['data'] = $data;
    successreturn($success);
}
