use std::{collections::BTreeMap, time::Duration};

/// Process profiling configuration.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct ProfilingConfig {
    /// Whether profiling should be started.
    pub enabled: bool,
    /// Pyroscope agent settings used when profiling is enabled.
    pub pyroscope: Option<PyroscopeConfig>,
}

impl ProfilingConfig {
    /// Creates an enabled Pyroscope profiling config.
    pub fn pyroscope(config: PyroscopeConfig) -> Self {
        Self {
            enabled: true,
            pyroscope: Some(config),
        }
    }
}

/// User-facing Pyroscope configuration.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PyroscopeConfig {
    /// Pyroscope server or Grafana Cloud Profiles endpoint.
    pub endpoint: String,
    /// Logical service name reported to Pyroscope.
    pub service_name: String,
    /// Sampling frequency in hertz. `0` is normalized to `100`.
    pub sample_rate: u32,
    /// Low-cardinality tags sent with every profile.
    pub tags: BTreeMap<String, String>,
    /// Maximum time a graceful shutdown may spend stopping the profiler.
    pub shutdown_timeout: Duration,
}

impl Default for PyroscopeConfig {
    fn default() -> Self {
        Self {
            endpoint: "http://127.0.0.1:4040".to_string(),
            service_name: "rs-zero-service".to_string(),
            sample_rate: 100,
            tags: BTreeMap::new(),
            shutdown_timeout: Duration::from_secs(5),
        }
    }
}

/// Normalized Pyroscope agent configuration.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PyroscopeAgentConfig {
    /// Pyroscope endpoint.
    pub endpoint: String,
    /// Service name.
    pub service_name: String,
    /// Effective sampling frequency.
    pub sample_rate: u32,
    /// Sanitized low-cardinality tags.
    pub tags: BTreeMap<String, String>,
    /// Shutdown timeout.
    pub shutdown_timeout: Duration,
}
