//! PostgreSQL installation with custom configuration
//!
//! This example demonstrates how to install PostgreSQL with custom
//! performance and security settings.

use lmrc_postgres::{PostgresConfig, PostgresManager};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    tracing_subscriber::fmt::init();

    // Build PostgreSQL configuration with custom settings
    let config = PostgresConfig::builder()
        .version("15")
        .database_name("production_db")
        .username("app_user")
        .password("strong_password_456")
        // Network configuration
        .listen_addresses("10.0.0.0/8") // Only allow private network
        .port(5432)
        // Performance tuning
        .max_connections(200)
        .shared_buffers("512MB")
        .effective_cache_size("2GB")
        .work_mem("16MB")
        .maintenance_work_mem("128MB")
        .wal_buffers("16MB")
        .checkpoint_completion_target(0.9)
        // Security
        .ssl(true)
        // Custom parameters
        .add_config("log_statement", "all")
        .add_config("log_duration", "on")
        .build()?;

    println!("Configuration: {:#?}", config);

    // Create manager with SSH key authentication
    let manager = PostgresManager::builder()
        .config(config)
        .server_ip("10.0.1.50")
        .ssh_user("admin")
        .ssh_key_path("/home/user/.ssh/id_rsa")
        .private_ip("10.0.1.50") // For connection string display
        .build()?;

    println!("Installing PostgreSQL with custom configuration...");

    // Step-by-step installation
    if !manager.is_installed().await? {
        println!("Installing PostgreSQL...");
        manager.install().await?;
    } else {
        println!("PostgreSQL is already installed");
    }

    println!("Configuring database...");
    manager.configure_database().await?;

    println!("Configuring server...");
    manager.configure_server().await?;

    println!("Testing connection...");
    manager.test_connection().await?;

    println!("✓ PostgreSQL is configured and ready!");

    Ok(())
}
