<?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/attendance/function.php';


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

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

// 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;
$institution_id = $jwt_validate->data->institution_id;
$branch_id = $jwt_validate->data->branch_id;

// Validate user access
$specific_db_name = validateStudentAcess($conn_rjllearning, $role, $workspace_id, ["principal"], null, null, $userid);
if (!$specific_db_name) {
    errorreturn('Access denied.');
} else {
    $conn_rjllearning_specific = dynamicDbConnection($specific_db_name);
}

$conn_rjllearning_specific = dynamicDbConnection($specific_db_name);

require_once $base_path_to_learning . 'functions/get_branch_functions.php';
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);


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


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

    $from_date = isset($postdata['from_date']) && !empty($postdata['from_date']) ? trim($postdata['from_date']) : date('Y-m-d');
    $to_date = isset($postdata['to_date']) && !empty($postdata['to_date']) ? trim($postdata['to_date']) : date('Y-m-d');

    if (!empty($from_date) && !empty($to_date) && ($from_date > $to_date)) {
        errorreturn('Invalid Date format');
    }

    $faculty_user_id = trim($postdata['faculty_user_id']);
    
    $sql = "SELECT * FROM faculty_info WHERE institution_id = ? and id =?";
    $stmt = $conn_rjllearning_specific->prepare($sql);
    $stmt->execute([$institution_id,$faculty_user_id]);
    $result = $stmt->fetch();
    
    if(!$result){
        errorreturn('Enter a valid faculty user id');
    }

    // Fetch attendance
    $success = getStaffAttendance($conn_rjllearning_specific, $institution_id, $branch_id, $faculty_user_id, $from_date, $to_date);

    successreturn($success);
}

?>
