<?php

ini_set('display_errors', 1);

require_once '../../../../resources/base_path_include.php';
require_once $base_path_to_resources . "includes/config.php"; // use require for mandatory files
require_once $base_path_to_resources . 'lib/function.php';
// require_once $base_path_to_resources . "lib/currency_functions.php";

//valdation file
require_once $base_path_to_resources . 'lib/validate_api_signature.php';
require_once $base_path_to_resources . 'lib/validate_function.php';
require_once $base_path . 'user_management/authenticate.php';

//creating dynamic db
require_once $base_path_to_resources . 'includes/specific_db_config.php';


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

if (
    !isset($postdata['company_id']) || !checkIfIssetAndNotEmpty($postdata['company_id']) ||
    !isset($postdata['branch_id']) || !checkIfIssetAndNotEmpty($postdata['branch_id']) ||
    !isset($postdata['book_id']) || !checkIfIssetAndNotEmpty($postdata['book_id']) ||
    !isset($postdata['function_type']) || !checkIfIssetAndNotEmpty($postdata['function_type']) ||
    !isset($postdata['jwt_token']) || !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;

function fetchSpecificBookTransactionSettings($specific_db_conn, $workspace_id, $company_id, $branch_id, $book_id)
{
    $sql = $specific_db_conn->prepare("SELECT transaction_numbering_system, additional_transaction_prefix FROM act_book_settings WHERE workspace_id = ? AND company_id = ? AND branch_id = ? AND book_id = ?");
    $sql->execute([$workspace_id, $company_id, $branch_id, $book_id]);
    $rows = $sql->fetchAll();

    if (count($rows) == 1) {
        return [
            'transaction_numbering_system' => json_decode($rows[0]['transaction_numbering_system'], true),
            'additional_transaction_prefix' => $rows[0]['additional_transaction_prefix']
        ];
    } else {
        errorreturn("Book settings not found.");
    }
}

if ($postdata['function_type'] == "transaction_book_setting") {
    // $function_code = $postdata['function_type'];
    $company_id = trim($postdata['company_id']);
    $branch_id = trim($postdata['branch_id']);
    $book_id = trim($postdata['book_id']);
    
    $confirmed_role_user = validatePrimaryAccess($conn, $workspace_id, $role, $userid, ['superadmin', 'admin', null]);

    if (!$confirmed_role_user) {
        errorreturn('Access denied.');
    } else {
        $specific_db_conn = dynamicDbConnection($confirmed_role_user);
        // if ($role == null) {
        //     checkCompanyBranchFunctionAccess($specific_db_conn, $role, $userid, $workspace_id, null, null, null, $postdata['function_type']);
        // }
    }
    $success['data'] = fetchSpecificBookTransactionSettings($specific_db_conn, $workspace_id, $company_id, $branch_id, $book_id);
    successreturn($success);
}
