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

require_once '../../../resources/base_path_include.php';

//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/validate_api_signature.php';
require_once $base_path_to_resources . 'lib/validate_function.php';
require_once $base_path . 'user_management/authenticate.php';

//function file
require_once $base_path . 'modules/accounting/book_management/basic_book_info_function.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();
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;
$username = $jwt_validate->data->username;


// mandatory function type should be upload_to_my_files
if (!isset($postdata['function_type']) or !checkIfIssetAndNotEmpty($postdata['function_type']) or $postdata['function_type'] != 'user_fetch_file_sharing' or !isset($postdata['search_string']) or !checkIfIssetAndNotEmpty($postdata['search_string'])) {
    errorreturn('Access denied.');
}

$search_user_share_with = trim($postdata['search_string']);

// $function_code = $postdata['function_type'];
$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);
        //  }
}

// superadmin / admin all users like
// no admin -> username 


if ($role == 'superadmin' or $role == 'admin') {
    $sql_fetch_user_list = "SELECT `name`, email FROM users where (email LIKE ? or name LIKE ?) and status = ? and username != ? order by name, email";
    $fetch_user_list = $conn->prepare($sql_fetch_user_list);

    $fetch_user_list->execute(array($search_user_share_with . "%", "%" . $search_user_share_with . "%", 1, $username));
    $result_user_list = $fetch_user_list->fetchAll();
    $user_list = [];
    if (count($result_user_list) > 0) {
        foreach ($result_user_list as $element) {
            $user_list[] = array('name' => $element['name'], 'email' => $element['email']);
        }
    }

    successreturn(array('user_options' => $user_list));
} else {
    $sql_fetch_user_list = "SELECT `name`, email FROM users where (email LIKE ? or name LIKE ?) and status = ?  and username != ? order by name, email";
    $fetch_user_list = $conn->prepare($sql_fetch_user_list);

    $fetch_user_list->execute(array($search_user_share_with, $search_user_share_with, 1, $username));
    $result_user_list = $fetch_user_list->fetchAll();
    $user_list = [];
    if (count($result_user_list) > 0) {
        foreach ($result_user_list as $element) {
            $user_list[] = array('name' => $element['name'], 'email' => $element['email']);
        }
    }

    successreturn(array('user_options' => $user_list));
}
