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

require_once '../../../../resources/base_path_include.php';
// should we not call header and ch
//valdation file
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";
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';
require_once $base_path_to_resources . 'includes/specific_db_config.php';




$postdata = json_decode(file_get_contents("php://input"), true);
checkIfNoPhpContentsReceive();
if (!isset($postdata['unit_id']) or !checkIfIssetAndNotEmpty($postdata['unit_id']) or !isset($postdata['company_id']) or !checkIfIssetAndNotEmpty($postdata['company_id']) or  !isset($postdata['branch_id']) or !checkIfIssetAndNotEmpty($postdata['branch_id']) or
!isset($postdata['book_id']) or !checkIfIssetAndNotEmpty($postdata['book_id']) 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']);
//authenticate jwt token
$jwttoken = $postdata['jwt_token'];
$jwt_validate = authenticate_jwt($jwttoken);
//then workspace_id is decoded in data of jwt use from there
$userid = $jwt_validate->data->id;
$workspace_id = $jwt_validate->data->workspace_id;
$role = $jwt_validate->data->role;

function getSpecificUnit($specific_db_conn, $unit_id, $workspace_id, $company_id, $branch_id, $book_id)
{

    // reeturns all data from the specific row of the profile
    $sql = $specific_db_conn->prepare("SELECT * FROM  unit_creation  where id = ? and workspace_id =? and company_id = ? and branch_id = ? and book_id = ?");
    $sql->execute(array($unit_id, $workspace_id, $company_id, $branch_id, $book_id));
    $rows = $sql->fetchAll();

    if (count($rows) == 1) {
        return array(
            'unit_id' => $rows[0]['id'], 'type' => $rows[0]['type'], 'symbol' => $rows[0]['symbol'],
            'formal_name' => $rows[0]['formal_name'], 'number_decimal_places' => $rows[0]['number_decimal_places'], 'secondary_unit' => $rows[0]['secondary_unit'], 'conversion' => $rows[0]['conversion']
        );
    } else {
        errorreturn("Enter Invalid Unit ID");
    }
}

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


    if (isset($postdata['unit_id']) and !empty($postdata['unit_id'])) {
        $unit_id = trim($postdata['unit_id']);
    } else {
        $unit_id = 'null';
    }
    $company_id = trim($postdata['company_id']);
    $branch_id = trim($postdata['branch_id']);
    $book_id = trim($postdata['book_id']);

    $function_code = 'fetch_unit'; // uses function code create_ledgers

    $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, $company_id, $branch_id, $book_id,  $function_code);
         }
    }
    $success['data'] = getSpecificUnit($specific_db_conn, $unit_id, $workspace_id, $company_id, $branch_id, $book_id);
    successreturn($success);
}
