<?php
ini_set('display_errors', 1);

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

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

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


if ( !isset($postdata['branch_id']) or !checkIfIssetAndNotEmpty($postdata['branch_id']) or
!isset($postdata['institution_id']) or !checkIfIssetAndNotEmpty($postdata['institution_id']) or
// !isset($postdata['marksheet_id']) or !checkIfIssetAndNotEmpty($postdata['marksheet_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.");
}
validateApiSignature($postdata['api_signature']);

$jwttoken = $postdata['jwt_token'];
$jwt_validate = authenticate_jwt($jwttoken);
//then workspace_id is iin decodede 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;

$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 getSpecificMarksheetTemplate($conn_rjllearning_specific,$institution_id,$branch_id,$marksheet_id) {
    
    $sql = $conn_rjllearning_specific->prepare("SELECT * FROM  marksheet_template where id= ? AND institution_id =? AND branch_id = ?");
    $sql->execute(array($marksheet_id,$institution_id,$branch_id));
    $result = $sql->fetchAll();
    if (count($result) >= 1) {
        $branch_info[] = getBranchInfo($conn_rjllearning_specific, $institution_id, $result[0]['branch_id']);
        $marksheet_info =  array('marksheet_id'=> $result[0]['id'],'institution_id'=> $result[0]['institution_id'],'branch_id'=> $branch_info,
        'marksheet_template_name'=> $result[0]['marksheet_template_name'],'marksheet_template_description'=> $result[0]['marksheet_template_description'],'color'=>json_decode($result[0]['color'],true),'header'=> $result[0]['header'],'body'=> $result[0]['body'],'footer'=> $result[0]['footer']);
        return $marksheet_info;
    }else{
          errorreturn("Marksheet template not found.");
    }

}

if($postdata['function_type']  == "marksheet_template") {  
    
    $marksheet_id = trim($postdata['marksheet_id']);
    $branch_id = trim($postdata['branch_id']);
    $institution_id = trim($postdata['institution_id']);    
    $success['data'] = getSpecificMarksheetTemplate($conn_rjllearning_specific,$institution_id,$branch_id,$marksheet_id);
    successreturn($success);
      
}

