//! Configuration management for typedialog
//!
//! Handles global configuration loading and defaults.
//! Provides unified backend configuration loading with CLI support.

pub mod cli_loader;
mod loader;

pub use cli_loader::load_backend_config;
pub use loader::load_global_config;

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::PathBuf;

/// Encryption configuration defaults
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EncryptionDefaults {
    /// Default encryption backend (age, rustyvault, sops)
    pub default_backend: Option<String>,
    /// Backend-specific configuration
    pub backend_config: HashMap<String, String>,
}

impl Default for EncryptionDefaults {
    fn default() -> Self {
        Self {
            default_backend: Some("age".to_string()),
            backend_config: HashMap::new(),
        }
    }
}

/// Global configuration for typedialog
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TypeDialogConfig {
    /// Default locale (e.g., "en-US", "es-ES")
    pub locale: Option<String>,

    /// Path to locales directory
    pub locales_path: PathBuf,

    /// Path to templates directory
    pub templates_path: PathBuf,

    /// Fallback locale when the requested locale is not available
    pub fallback_locale: String,

    /// Encryption configuration
    #[serde(default)]
    pub encryption: Option<EncryptionDefaults>,
}

impl Default for TypeDialogConfig {
    fn default() -> Self {
        Self {
            locale: None,
            locales_path: PathBuf::from("./examples/06-i18n"),
            templates_path: PathBuf::from("./templates"),
            fallback_locale: "en-US".to_string(),
            encryption: None,
        }
    }
}

impl TypeDialogConfig {
    /// Create a new config with default values
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the default locale
    pub fn with_locale(mut self, locale: impl Into<String>) -> Self {
        self.locale = Some(locale.into());
        self
    }

    /// Set the locales directory path
    pub fn with_locales_path(mut self, path: PathBuf) -> Self {
        self.locales_path = path;
        self
    }

    /// Set the templates directory path
    pub fn with_templates_path(mut self, path: PathBuf) -> Self {
        self.templates_path = path;
        self
    }

    /// Set the fallback locale
    pub fn with_fallback_locale(mut self, locale: impl Into<String>) -> Self {
        self.fallback_locale = locale.into();
        self
    }
}
