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

// Validation file
require_once $base_path_to_learning_resources . 'lib/validate_function.php';
require_once $base_path_to_learning . '/user_management/check_username_availability.php';
require_once $base_path_to_learning . 'user_management/authenticate.php';

require_once $base_path_to_learning_resources . 'includes/specific_db_config.php';
require_once $base_path_to_learning . 'functions/common_functions.php';

require_once $base_path_to_learning_resources . 'external_requests/finware_curl/groups_ledgers/curl_function_branch_students.php';
require_once $base_path_to_learning_resources . 'external_requests/finware_curl/fees_management/curl_fees_creation_finware.php';


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

if (
    !isset($postdata['institution_id']) || !checkIfIssetAndNotEmpty($postdata['institution_id']) ||
    !isset($postdata['branch_id']) || !checkIfIssetAndNotEmpty($postdata['branch_id']) ||
    !isset($postdata['api_signature']) || !checkIfIssetAndNotEmpty($postdata['api_signature']) ||
    !isset($postdata['function_type']) || !checkIfIssetAndNotEmpty($postdata['function_type']) ||
    !isset($postdata['jwt_token']) || !checkIfIssetAndNotEmpty($postdata['jwt_token'])
) {
    errorreturn("All required fields must be provided.");
}

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;
$username = $jwt_validate->data->username;
$role = $jwt_validate->data->role;

$institution_id = $postdata['institution_id'];
$branch_id = $postdata['branch_id'];
$ledger_name = ucwords($postdata['ledger_name']);
$ledger_parent_group = $postdata['ledger_parent_group'];
$product_connection_param1 = (isset($postdata['product_connection_param1']) and !empty($postdata['product_connection_param1'])) ? ($postdata['product_connection_param1']) : null;

$specific_db_name = validateUserAccess($conn_rjllearning, $userid, $role, $workspace_id,["teacher"], null);
if (!$specific_db_name) {
    errorreturn('Access denied.');
} else {
    $conn_rjllearning_specific = dynamicDbConnection($specific_db_name);
}

if ($postdata['function_type'] == 'create_fees') {
    
    // Check if the branch exists
    $branch = getBranchDetails($conn_rjllearning_specific, $branch_id, $institution_id);
    
    if ($branch) {
        
        $rejoice_solutions_company_id = $branch['rejoice_solutions_company_id'];
        $rejoice_solutions_branch_id = $branch['rejoice_solutions_branch_id'];
        $rejoice_finware_book_id = $branch['rejoice_finware_book_id'];
        
        $ledgers_array = array("workspace_id"=>$workspace_id,"created_by"=>$username,"company_id"=>$rejoice_solutions_company_id,"branch_id"=>$rejoice_solutions_branch_id,"book_id"=>$rejoice_finware_book_id,"ledger_name"=>$ledger_name,"ledger_parent_group"=>$ledger_parent_group,
        "product_connection_param1"=>$product_connection_param1,"function_type"=>"add");
        
        CurlFeesCreateAccount($ledgers_array);
        successreturn("Ledgers added Successfully");
        
        
    }else{
        errorreturn('Finware database connection is not established with learning.');
    }
}
