<?php
// Enable error display for debugging
ini_set('display_errors', 1);

// Include necessary 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 . 'functions/common_functions.php';
require_once $base_path_to_learning_resources . 'includes/specific_db_config.php';

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

// Check if no data was received
checkIfNoPhpContentsReceive();

// Validate required fields
if (!isset($postdata['institution_id']) or !checkIfIssetAndNotEmpty($postdata['institution_id']) or
    !isset($postdata['marksheet_id']) or !checkIfIssetAndNotEmpty($postdata['marksheet_id']) or
!isset($postdata['branch_id']) or !checkIfIssetAndNotEmpty($postdata['branch_id']) or
!isset($postdata['print_logo_bool']) or !checkIfIssetAndNotEmpty($postdata['print_logo_bool']) or
!isset($postdata['report_print_institution_name']) or !checkIfIssetAndNotEmpty($postdata['report_print_institution_name']) or
!isset($postdata['report_print_branch_name']) or !checkIfIssetAndNotEmpty($postdata['report_print_branch_name']) or
!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.");
}

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

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

// Extract user details 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 and get specific database name
$specific_db_name = validateUserAccess($conn_rjllearning, $userid, $role, $workspace_id, null);
if (!$specific_db_name) {
    errorreturn('Access denied.');
} else {
    $conn_rjllearning_specific = dynamicDbConnection($specific_db_name);
}

$marksheet_id = trim($postdata['marksheet_id']);
$institution_id = trim($postdata['institution_id']);
$branch_id = trim($postdata['branch_id']);
$print_logo_bool = trim($postdata['print_logo_bool']);
$report_print_institution_name = trim($postdata['report_print_institution_name']);
$report_print_branch_name = trim($postdata['report_print_branch_name']);
$status = 1;
$last_updated_time = date("Y-m-d H:i:s", time());
// Check if academic session is active
$check_academic_confirmation = CheckAcademicSession($conn_rjllearning_specific, $institution_id);
if (!$check_academic_confirmation) {
    errorreturn("Access denied. Academic session not active.");
}

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

    $ExecuteSqlSettings = $conn_rjllearning_specific->prepare("SELECT id, institution_id FROM marksheet_settings WHERE id = ? AND institution_id = ? AND branch_id = ?");
    $ExecuteSqlSettings->execute([$marksheet_id, $institution_id,$branch_id]);
    $ResultSettings = $ExecuteSqlSettings->fetchAll();
 
    if ($ResultSettings < 1) {
        errorreturn("Institution marksheet setting not exists.");
    }

    $sql = "UPDATE marksheet_settings SET  print_logo_bool=?,report_print_institution_name= ?,report_print_branch_name = ?,status = ?, last_modified_by=?, last_updated_time=?,last_modified_at = ? WHERE id = ? AND institution_id = ? and branch_id = ?";
    $stmt = $conn_rjllearning_specific->prepare($sql);
    $stmt->execute([$print_logo_bool,$report_print_institution_name,$report_print_branch_name,$status, $username,$last_updated_time,date('Y-m-d'), $marksheet_id, $institution_id,$branch_id]);
    successreturn("Updated print settings successfully.");

}
?>
