//! Tests for configuration parsing.

use super::*;

#[test]
fn test_default_config() {
    let config = AphoriaConfig::default();

    assert_eq!(config.thresholds.block, 0.7);
    assert_eq!(config.thresholds.flag, 0.4);
    assert!(config.extractors.enabled.contains(&"tls_verify".to_string()));
    assert!(config.scan.exclude.contains(&"target/".to_string()));
    assert!(config.policies.is_empty());
    assert!(!config.hosted.is_enabled());
}

#[test]
fn test_config_parse() {
    // Note: Top-level keys must appear before any section headers in TOML
    let toml = r#"
# Top-level policies (must be before any [section] headers)
policies = [
    "file://./policies/security.pack",
    "https://example.com/policies/base.pack"
]

[project]
name = "testproject"
language = "rust"

[thresholds]
block = 0.8
flag = 0.5

[scan]
exclude = ["build/", "dist/"]
"#;

    let config: AphoriaConfig = toml::from_str(toml).expect("should parse");

    assert_eq!(config.project.name, Some("testproject".to_string()));
    assert_eq!(config.project.language, Some("rust".to_string()));
    assert_eq!(config.thresholds.block, 0.8);
    assert_eq!(config.thresholds.flag, 0.5);
    assert!(config.scan.exclude.contains(&"build/".to_string()));
    assert_eq!(config.policies.len(), 2);
    assert_eq!(config.policies[0], "file://./policies/security.pack");
}

#[test]
fn test_hosted_config_parse() {
    let toml = r#"
[project]
name = "billing-service"

[hosted]
url = "https://episteme.acme.corp"
project_id = "billing-api"
team_id = "platform-team"
sync_mode = "local-and-remote"
offline_fallback = "fail"
max_retries = 5
retry_delay_ms = 2000
api_key_env = "CUSTOM_API_KEY"
"#;

    let config: AphoriaConfig = toml::from_str(toml).expect("should parse");

    assert!(config.hosted.is_enabled());
    assert_eq!(config.hosted.url, Some("https://episteme.acme.corp".to_string()));
    assert_eq!(config.hosted.project_id, Some("billing-api".to_string()));
    assert_eq!(config.hosted.team_id, Some("platform-team".to_string()));
    assert_eq!(config.hosted.sync_mode, SyncMode::LocalAndRemote);
    assert_eq!(config.hosted.offline_fallback, OfflineFallback::Fail);
    assert_eq!(config.hosted.max_retries, 5);
    assert_eq!(config.hosted.retry_delay_ms, 2000);
    assert_eq!(config.hosted.api_key_env, "CUSTOM_API_KEY");
}

#[test]
fn test_hosted_config_defaults() {
    let toml = r#"
[hosted]
url = "https://episteme.acme.corp"
"#;

    let config: AphoriaConfig = toml::from_str(toml).expect("should parse");

    assert!(config.hosted.is_enabled());
    assert_eq!(config.hosted.url, Some("https://episteme.acme.corp".to_string()));
    assert_eq!(config.hosted.project_id, None);
    assert_eq!(config.hosted.team_id, None);
    assert_eq!(config.hosted.sync_mode, SyncMode::RemoteOnly);
    assert_eq!(config.hosted.offline_fallback, OfflineFallback::Skip);
    assert_eq!(config.hosted.max_retries, 3);
    assert_eq!(config.hosted.retry_delay_ms, 1000);
    assert_eq!(config.hosted.api_key_env, "APHORIA_API_KEY");
}

#[test]
fn test_hosted_not_enabled_without_url() {
    let config = AphoriaConfig::default();
    assert!(!config.hosted.is_enabled());

    let toml = r#"
[hosted]
project_id = "test"
"#;
    let config: AphoriaConfig = toml::from_str(toml).expect("should parse");
    assert!(!config.hosted.is_enabled());
}
