//! Configuration types for storage initialization.
//!
//! This module provides config structs that allow components to be initialized
//! with sensible defaults while still allowing customization when needed.

use std::path::PathBuf;

use super::dir;

/// Configuration for storage backends.
///
/// This struct allows components to configure storage without needing to know
/// implementation details like SQLite. Users provide *what* they want (path, options),
/// not *how* it's implemented.
///
/// # Example
///
/// ```
/// use mi6_core::StorageConfig;
///
/// // Use defaults (reads from MI6_DIR_PATH or ~/.mi6)
/// let config = StorageConfig::default();
///
/// // Custom database path
/// let config = StorageConfig {
///     db_path: Some("/tmp/test.db".into()),
///     ..Default::default()
/// };
/// ```
#[derive(Debug, Clone, Default)]
pub struct StorageConfig {
    /// Database path. If None, uses the default location.
    ///
    /// Resolution order (when calling `resolve_db_path()`):
    /// 1. This field if set
    /// 2. Default: `<mi6_dir>/mi6.db`
    pub db_path: Option<PathBuf>,
}

impl StorageConfig {
    /// Create a new storage config with defaults.
    pub fn new() -> Self {
        Self::default()
    }

    /// Create a storage config with a specific database path.
    pub fn with_path(path: impl Into<PathBuf>) -> Self {
        Self {
            db_path: Some(path.into()),
        }
    }

    /// Get the effective database path, resolving defaults if needed.
    ///
    /// Resolution order:
    /// 1. `db_path` field if set
    /// 2. Default: `<mi6_dir>/mi6.db`
    pub fn resolve_db_path(&self) -> Result<PathBuf, crate::ConfigError> {
        if let Some(ref path) = self.db_path {
            return Ok(path.clone());
        }
        dir::db_path()
    }
}

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

    #[test]
    fn test_storage_config_default() {
        let config = StorageConfig::default();
        assert!(config.db_path.is_none());
    }

    #[test]
    fn test_storage_config_with_path() {
        let config = StorageConfig::with_path("/tmp/test.db");
        assert_eq!(config.db_path, Some(PathBuf::from("/tmp/test.db")));
    }
}
