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

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

if (

    !isset($postdata['api_signature']) or !checkIfIssetAndNotEmpty($postdata['api_signature']) or
    !isset($postdata['student_planner_id']) or !checkIfIssetAndNotEmpty($postdata['student_planner_id']) or
    !isset($postdata['jwt_token']) or  !checkIfIssetAndNotEmpty($postdata['jwt_token'])
) {
    errorreturn("All * fields are required.");
}
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;
$role = $jwt_validate->data->role;
$branch_id = $jwt_validate->data->branch_id;
$institution_id = $jwt_validate->data->institution_id;
$specific_db_name = validateUserAccess($conn_rjllearning, $userid, $role, $workspace_id, ['superadmin', 'admin'], null);
if (!$specific_db_name) {
    errorreturn('Access denied.');
} else {
    $conn_rjllearning_specific = dynamicDbConnection($specific_db_name);
}

function get_specific_student_planner($conn_rjllearning_specific, $institution_id, $planner_id)
{
    $sql = "SELECT * FROM student_planner WHERE institution_id = ? AND id = ?";
    $stmt = $conn_rjllearning_specific->prepare($sql);
    $stmt->execute([$institution_id, $planner_id]);
    $row = $stmt->fetch();
    
    if (!$row) {
        errorreturn("No data available");
    }
    
    return array(
        'student_planner_id' => $row['id'],
        'institution_id' => $row['institution_id'],
        'academic_session' => $row['academic_session'],
        'from_date' => $row['from_date'],
        'to_date' => $row['to_date'],
        'schedule_details' => json_decode($row['schedule_details'], true),
        'remarks' => $row['remarks'],
        'date_created' => $row['date_created'],
        'created_by' => $row['created_by'],
        'last_modified_by' => $row['last_modified_by'],
        'last_modified_at' => $row['last_modified_at'],
        'status' => $row['status']
    );
}

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

    $planner_id = $postdata['student_planner_id'];
    
    $success['data'] = get_specific_student_planner($conn_rjllearning_specific, $institution_id, $planner_id);
    successreturn($success);
}
