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

require_once $base_path_to_learning_resources . 'template/marksheet/header/header1.php';
require_once $base_path_to_learning_resources . 'template/marksheet/header/header2.php';
require_once $base_path_to_learning_resources . 'template/marksheet/header/header3.php';
require_once $base_path_to_learning . 'admin_api/global_settings/marksheet/marksheet_template/fetch_specific_marksheet.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['branch_id']) or !checkIfIssetAndNotEmpty($postdata['branch_id']) 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);
}

function getMarksheetHeader($conn_rjllearning_specific, $conn_rjllearning, $institution_id,$branch_id, $type,$marksheet_id) {
   
    $additional_data = ['branch_code','pincode','address','contact_number','email'];
    $header = ''; // Initialize $header
    
    if ($type === 'all') {
        // Capture the output of all header templates
        ob_start();
        $header1 = getHeader1($conn_rjllearning, $conn_rjllearning_specific, $institution_id, $branch_id, $additional_data,$marksheet_id);
        $header3 = getHeader3($conn_rjllearning, $conn_rjllearning_specific, $institution_id, $branch_id, $additional_data,$marksheet_id);
        $header2 = getHeader2($conn_rjllearning, $conn_rjllearning_specific, $institution_id, $branch_id, $additional_data,$marksheet_id);

         $header .= $header1 . $header2 . $header3;
        //echo $header;
        ob_end_clean(); // Clear the buffer
    } else {

     switch ($type) {
            case 'header1':
                $header = getHeader1($conn_rjllearning, $conn_rjllearning_specific, $institution_id, $branch_id, $additional_data,$marksheet_id);
                break;

            case 'header2':
                $header = getHeader2($conn_rjllearning, $conn_rjllearning_specific, $institution_id, $branch_id, $additional_data,$marksheet_id);
                break;

            case 'header3':
                $header = getHeader3($conn_rjllearning, $conn_rjllearning_specific, $institution_id, $branch_id, $additional_data,$marksheet_id);
                break;

            default:
                // Handle unknown types or errors here
                $header = 'Unknown header type';
                break;
     }
    }
    
    echo $header;
}


if ($postdata['function_type'] == "fetch_header") {
    
    $institution_id = trim($postdata['institution_id']);
    $branch_id = trim($postdata['branch_id']);
    $type = trim($postdata['type']);
    $marksheet_id = (isset($postdata['marksheet_id']) and !empty($postdata['marksheet_id'])) ? (trim($postdata['marksheet_id'])) : null;
    
    getMarksheetHeader($conn_rjllearning_specific,$conn_rjllearning, $institution_id,$branch_id, $type,$marksheet_id);
}