/*-------------------------------------------------------------------------
 * Copyright (c) Microsoft Corporation.  All rights reserved.
 *
 * documentdb_tests/src/test_setup/config.rs
 *
 *-------------------------------------------------------------------------
 */

use documentdb_gateway_core::configuration::{
    CertInputType, CertificateOptions, DocumentDBSetupConfiguration,
};

#[must_use]
pub fn setup_configuration() -> DocumentDBSetupConfiguration {
    let system_user = std::env::var("PostgresSystemUser").unwrap_or(whoami::username());

    DocumentDBSetupConfiguration {
        node_host_name: "localhost".to_owned(),
        blocked_role_prefixes: Vec::new(),
        gateway_listen_port: Some(10260),
        allow_transaction_snapshot: Some(false),
        certificate_options: CertificateOptions {
            cert_type: CertInputType::PemAutoGenerated,
            ..Default::default()
        },
        postgres_system_user: system_user.clone(),
        postgres_data_user: system_user,
        ..Default::default()
    }
}

/// Returns a baseline configuration with `postgres_command_timeout_secs`
/// overridden to the supplied value.
#[must_use]
pub fn setup_configuration_with_command_timeout(
    postgres_command_timeout_secs: u64,
) -> DocumentDBSetupConfiguration {
    DocumentDBSetupConfiguration {
        postgres_command_timeout_secs: Some(postgres_command_timeout_secs),
        ..setup_configuration()
    }
}

/// Returns a setup configuration that points at an unreachable `PostgreSQL`
/// host so connection creation deterministically fails.
#[must_use]
pub fn failing_setup_configuration() -> DocumentDBSetupConfiguration {
    DocumentDBSetupConfiguration {
        postgres_host_name: Some("127.0.0.1".to_owned()),
        postgres_port: Some(1),
        postgres_command_timeout_secs: Some(1),
        ..setup_configuration()
    }
}

#[must_use]
pub fn setup_configuration_with_unix_socket_custom(
    path: Option<String>,
    permissions: Option<String>,
) -> DocumentDBSetupConfiguration {
    let mut config = setup_configuration();
    config.unix_socket_path = path;
    config.unix_socket_file_permissions = permissions;
    config
}
