//! Server configuration

use serde::{Deserialize, Serialize};
use std::net::SocketAddr;
use plasmavmc_types::{FireCrackerConfig, KvmConfig};

/// TLS configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TlsConfig {
    /// Path to certificate file (PEM)
    pub cert_file: String,

    /// Path to private key file (PEM)
    pub key_file: String,

    /// Path to CA certificate for client verification (optional, for mTLS)
    pub ca_file: Option<String>,

    /// Require client certificates (mTLS)
    #[serde(default)]
    pub require_client_cert: bool,
}

/// Server configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServerConfig {
    /// Address to listen on
    pub addr: SocketAddr,
    /// HTTP REST API listen address
    #[serde(default = "default_http_addr")]
    pub http_addr: SocketAddr,
    /// Log level
    pub log_level: String,
    /// TLS configuration (optional)
    pub tls: Option<TlsConfig>,
    /// Authentication configuration
    #[serde(default)]
    pub auth: AuthConfig,
    /// Configuration for KVM backend
    #[serde(default)]
    pub kvm: KvmConfig,
    /// Configuration for FireCracker backend
    #[serde(default)]
    pub firecracker: FireCrackerConfig,
}

fn default_http_addr() -> SocketAddr {
    "127.0.0.1:8084".parse().unwrap()
}

/// Authentication configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuthConfig {
    /// IAM server endpoint
    #[serde(default = "default_iam_server_addr")]
    pub iam_server_addr: String,
}

fn default_iam_server_addr() -> String {
    "127.0.0.1:50051".to_string()
}

impl Default for AuthConfig {
    fn default() -> Self {
        Self {
            iam_server_addr: default_iam_server_addr(),
        }
    }
}

impl Default for ServerConfig {
    fn default() -> Self {
        Self {
            addr: "0.0.0.0:8080".parse().unwrap(),
            http_addr: default_http_addr(),
            log_level: "info".to_string(),
            tls: None,
            auth: AuthConfig::default(),
            kvm: KvmConfig::default(),
            firecracker: FireCrackerConfig::default(),
        }
    }
}
