//! WAL configuration.
//!
//! Extracted from `mod.rs` to keep the module root focused on the public
//! handle API (`WalHandle`, `WalSender`, `SignalEvent`).

use std::path::PathBuf;
use std::time::Duration;

/// Default segment size: 16 MB.
const DEFAULT_SEGMENT_SIZE: u64 = 16 * 1024 * 1024;

/// Default batch size: up to 100 events per batch.
const DEFAULT_BATCH_SIZE: usize = 100;

/// Default batch timeout: 10 milliseconds.
const DEFAULT_BATCH_TIMEOUT: Duration = Duration::from_millis(10);

/// Default dedup window: 30 seconds (double-buffered, so effective window is ~60s).
const DEFAULT_DEDUP_WINDOW: Duration = Duration::from_secs(30);

/// Configuration for the WAL.
#[derive(Debug, Clone)]
pub struct WalConfig {
    /// Base directory for WAL data. Segment files and checkpoint metadata
    /// are stored in `{dir}/wal/`.
    pub dir: PathBuf,
    /// Maximum segment file size in bytes before rotation.
    pub segment_size: u64,
    /// Maximum number of events per batch.
    pub batch_size: usize,
    /// Maximum time to wait before flushing a partial batch.
    pub batch_timeout: Duration,
    /// Duration for the dedup window rotation.
    pub dedup_window: Duration,
}

impl Default for WalConfig {
    fn default() -> Self {
        Self {
            dir: PathBuf::from("data"),
            segment_size: DEFAULT_SEGMENT_SIZE,
            batch_size: DEFAULT_BATCH_SIZE,
            batch_timeout: DEFAULT_BATCH_TIMEOUT,
            dedup_window: DEFAULT_DEDUP_WINDOW,
        }
    }
}

impl WalConfig {
    /// The actual WAL directory path: `{self.dir}/wal/`.
    #[must_use]
    pub fn wal_dir(&self) -> PathBuf {
        self.dir.join("wal")
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;

    #[test]
    fn default_config_values() {
        let config = WalConfig::default();
        assert_eq!(config.segment_size, 16 * 1024 * 1024);
        assert_eq!(config.batch_size, 100);
        assert_eq!(config.batch_timeout, Duration::from_millis(10));
        assert_eq!(config.dedup_window, Duration::from_secs(30));
    }

    #[test]
    fn wal_dir_appends_wal_suffix() {
        let config = WalConfig {
            dir: PathBuf::from("/tmp/mydata"),
            ..WalConfig::default()
        };
        assert_eq!(config.wal_dir(), PathBuf::from("/tmp/mydata/wal"));
    }
}
