<?php

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

// Validation 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';
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 . 'students_api/modules/test/student_test_attempt/student_test_attempt_function.php';

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

// Validate required fields
$required_fields = ['institution_id', 'branch_id', 'test_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']);

$jwttoken = $postdata['jwt_token'];
$jwt_validate = authenticate_jwt($jwttoken);
// then workspace_id is in decoded data of jwt use from there
$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 = validateStudentAcess($conn_rjllearning, $role, $workspace_id, ["student"], null, null);

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

require_once $base_path_to_learning . 'functions/get_branch_functions.php';

$institution_id = trim($postdata['institution_id']);
$branch_id = trim($postdata['branch_id']);
$class_id = trim($postdata['class_id']);
$section_id = trim($postdata['section_id']);
$test_id = trim($postdata['test_id']);
$student_id = $userid; // Assuming you have student_id in $postdata


$exam_start_time = (new DateTime())->format('H:i:s');
$exit_time = (new DateTime())->format('H:i:s');
$date_created = date('Y-m-d');
$time_created = time();
$status = 0;

if ($postdata['function_type'] == 'create_test_submission') {
        $test = fetchTestdetails($conn_rjllearning_specific, $test_id, $institution_id, $branch_id);
    if (!$test) {
        successReturn("Test does not exist.");
    }
    
    $display_question_by = $test['display_question_by'];
    
    $result = startOrContinueTest($conn_rjllearning_specific, $institution_id, $branch_id, $test_id, $class_id, $section_id, $student_id, $display_question_by);
    if ($result !== false) {
        successReturn(["test_started" => $result]);
    } else {
        errorReturn("Insert failed.");
    }
}

?>
