<?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';
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';
require_once $base_path_to_learning_resources . 'includes/specific_db_config.php';

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

/*
 * Required Checks
 */
if (
    !isset($postdata['api_signature']) or !checkIfIssetAndNotEmpty($postdata['api_signature']) or
    !isset($postdata['function_type']) or !checkIfIssetAndNotEmpty($postdata['function_type']) or
    !isset($postdata['jwt_token']) or !checkIfIssetAndNotEmpty($postdata['jwt_token'])
) {
    errorreturn("All * fields are required.");
}
validateApiSignature($postdata['api_signature']);

/*
 * JWT Validation
 */
$jwttoken   = $postdata['jwt_token'];
$jwt_validate = authenticate_jwt($jwttoken);
$userid       = $jwt_validate->data->id;
$workspace_id = $jwt_validate->data->workspace_id;
$role         = $jwt_validate->data->role;
$institution_id = $jwt_validate->data->institution_id;
$branch_id      = $jwt_validate->data->branch_id;
$class_id       = $jwt_validate->data->class;
$section_id     = $jwt_validate->data->section;

/*
 * DB Access Validation
 */
$specific_db_name = validateStudentAcess($conn_rjllearning, $role, $workspace_id, ["student"], null, null, $userid);
if (!$specific_db_name) {
    errorreturn('Access denied.');
} else {
    $conn_rjllearning_specific = dynamicDbConnection($specific_db_name);
}
validateStudentClassSection($conn_rjllearning_specific, $userid, $institution_id, $branch_id, $class_id, $section_id);

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

/*
 * Function Handling
 */
if ($postdata['function_type'] == "fetch_specific_speech_submission") {

    if (!isset($postdata['submission_id']) || empty($postdata['submission_id'])) {
        errorreturn("submission_id is required.");
    }

    $submission_id = (int)$postdata['submission_id'];

    // ---- Fetch Single Submission ----
    $sql = "SELECT * FROM speech_tasks_submission WHERE id = ? AND student_id = ? AND institution_id = ? AND academic_id = ? AND workspace_id = ? AND branch_id = ? LIMIT 1";
    $params = [$submission_id, $userid, $institution_id, $academic_id, $workspace_id, $branch_id];

    $stmt = $conn_rjllearning->prepare($sql);
    $stmt->execute($params);
    $submission = $stmt->fetch(PDO::FETCH_ASSOC);

    if (!$submission) {
        errorreturn("Submission not found.");
    }

    successreturn([
        'data' => $submission
    ]);

} else {
    errorreturn("Invalid function type.");
}
?>