//! Handling of common items used in configuration files.

use garde::Validate;
use serde::{Deserialize, Serialize};

use crate::{ConfigOrigin, file::ConfigOpenpgpSettings};

/// Settings for all supported cryptographic technologies.
///
/// Describes zero or more settings for supported cryptographic technologies in a configuration
/// file. May be used globally, or on an OS-specific level.
///
/// # Note
///
/// This struct _must_ be validated after creation.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize, Validate)]
#[serde(rename_all = "lowercase")]
pub struct ConfigTechnologySettings {
    #[serde(skip)]
    #[garde(skip)]
    pub(crate) origins: Vec<ConfigOrigin>,

    #[garde(dive)]
    openpgp: Option<ConfigOpenpgpSettings>,
}

impl ConfigTechnologySettings {
    /// Creates a new [`ConfigTechnologySettings`].
    // NOTE: used in tests.
    #[allow(dead_code)]
    pub(crate) fn new(origins: Vec<ConfigOrigin>, openpgp: Option<ConfigOpenpgpSettings>) -> Self {
        Self { origins, openpgp }
    }

    /// Returns a reference to the [`ConfigOpenpgpSettings`].
    pub(crate) fn config_openpgp_settings(&self) -> Option<&ConfigOpenpgpSettings> {
        self.openpgp.as_ref()
    }

    /// Returns a reference to the list of config file origins.
    pub fn origins(&self) -> &[ConfigOrigin] {
        &self.origins
    }
}

impl Default for ConfigTechnologySettings {
    fn default() -> Self {
        Self {
            origins: vec![ConfigOrigin::Default],
            openpgp: Some(ConfigOpenpgpSettings::default()),
        }
    }
}
