//! Configuration loading, parsing, and validation for Bivvy.
//!
//! This module handles all aspects of configuration:
//! - Schema definitions in [`schema`]
//! - File discovery and loading in [`loader`]
//! - Deep merging in [`merger`]
//! - Validation in [`validator`]
//! - Variable interpolation in [`interpolation`]
//! - Environment variable handling in [`environment`]
//!
//! # Example
//!
//! ```
//! use bivvy::config::{load_merged_config, validate};
//! use tempfile::TempDir;
//! use std::fs;
//!
//! let temp = TempDir::new().unwrap();
//! let bivvy_dir = temp.path().join(".bivvy");
//! fs::create_dir_all(&bivvy_dir).unwrap();
//! fs::write(bivvy_dir.join("config.yml"), "app_name: test").unwrap();
//!
//! let config = load_merged_config(temp.path()).unwrap();
//! validate(&config).unwrap();
//! assert_eq!(config.app_name, Some("test".to_string()));
//! ```
//!
//! # Configuration File Locations
//!
//! Bivvy discovers and merges configuration in this order:
//! 1. Remote base configs (from `extends:`)
//! 2. User global config (`~/.bivvy/config.yml`)
//! 3. Project config (`.bivvy/config.yml`)
//! 4. Split-file steps (`.bivvy/steps/*.yml`)
//! 5. Split-file workflows (`.bivvy/workflows/*.yml`)
//! 6. Local overrides (`.bivvy/config.local.yml`)

pub mod discovery;
pub mod env_file;
pub mod env_layer;
pub mod environment;
pub mod extends;
pub mod interpolation;
pub mod loader;
pub mod merger;
pub mod remote;
pub mod schema;
pub mod trust;
pub mod validator;
pub mod vars;

// Schema re-exports
pub use schema::{
    BehaviorConfig, BivvyConfig, CustomRequirement, CustomRequirementCheck, EnvVarSettings,
    EnvironmentProfileSettings, EnvironmentScopingConfig, EnvironmentVarsConfig, ExecutionConfig,
    ExecutionSettings, HookConfig, OutputMode, PromptConfig, PromptType, SecretConfig, Settings,
    StepConfig, StepEnvironmentOverride, StepOutputConfig, StepOutputSettings, StepOverride,
    TemplateSource, VarDefinition, WorkflowConfig, WorkflowFile, WorkflowSettings,
};

// Discovery re-exports
pub use discovery::{Discovery, WorkflowHeader};

// Check type re-exports (new check system)
pub use crate::checks::{
    BaselineConfig, ChangeKind, Check, CheckOutcome, CheckResult as NewCheckResult, OnChange,
    PresenceKind, SatisfactionCondition, SizeLimit, SnapshotScope, ValidationMode,
};

// Vars re-exports
pub use vars::evaluate_vars;

// Loader re-exports
pub use loader::{
    ensure_global_config, find_project_root, load_config, load_config_file, load_config_value,
    load_for_run, load_for_run_with_resolver, load_for_run_with_trust, load_merged_config,
    load_merged_config_with_resolver, load_merged_config_with_trust, load_project_config,
    load_single_step_file, load_single_workflow_file, parse_config, parse_workflow_file_value,
    ConfigPaths,
};

// Merger re-exports
pub use merger::{deep_merge, merge_configs};

// Validator re-exports
pub use validator::{validate, validate_config, ValidationError};

// Interpolation re-exports
pub use interpolation::{
    extract_variables, has_interpolation, parse_interpolation, resolve_string,
    resolve_string_with_default, InterpolationContext, Segment,
};

// Environment re-exports
pub use environment::{
    is_secret, load_env_file, load_env_file_optional, load_system_env, merge_env, SECRET_PATTERNS,
};

// Remote re-exports
pub use remote::{resolve_auth, AuthHeader, RemoteFetcher};

// Extends re-exports
pub use extends::{validate_extends, ExtendsResolver};

// Trust re-exports
pub use trust::{TrustPolicy, TrustStore};

// Env layer re-exports
pub use env_layer::{build_yaml_env_stack, EnvLayer, EnvLayerStack};

// Env file re-exports
pub use env_file::EnvFileParser;

#[cfg(test)]
mod tests {
    #[test]
    fn serde_yaml_parses_basic_yaml() {
        let yaml = "name: test\nvalue: 42";
        let parsed: serde_yaml::Value = serde_yaml::from_str(yaml).unwrap();
        assert_eq!(parsed["name"], "test");
        assert_eq!(parsed["value"], 42);
    }

    #[test]
    fn serde_yaml_handles_nested_structures() {
        let yaml = r#"
          settings:
            output: verbose
            logging: true
          steps:
            - name: first
            - name: second
        "#;
        let parsed: serde_yaml::Value = serde_yaml::from_str(yaml).unwrap();
        assert_eq!(parsed["settings"]["output"], "verbose");
        assert_eq!(parsed["steps"][0]["name"], "first");
    }
}
