//! Example demonstrating configuration discovery with diagnostics.
//!
//! Run with:
//!   cargo run --example discovery

use cfgmatic::{
    ConfigManager, DiscoveryOptions, FilePattern, config_file_path, config_path, discover_config,
};

fn main() {
    let app_name = "myapp";

    println!("=== Configuration Discovery Example ===\n");

    // 1. Quick discovery using convenience function
    println!("1. Quick Discovery:");
    let discovery = discover_config(app_name);

    println!(
        "   Preferred path for new config: {}",
        discovery.preferred_path.display()
    );

    if let Some(found) = &discovery.found_path {
        println!("   Found existing config at: {}", found.display());
    } else {
        println!("   No existing config found");
    }

    println!("\n   Searched locations:");
    for candidate in &discovery.candidates {
        let status_icon = match candidate.status {
            cfgmatic::PathStatus::Directory => "D",
            cfgmatic::PathStatus::File => "F",
            cfgmatic::PathStatus::NotFound => "-",
        };
        println!(
            "     [{}] {:?}: {}",
            status_icon,
            candidate.tier,
            candidate.path.display()
        );
    }

    // 2. Using ConfigManager for discovery
    println!("\n2. Using ConfigManager:");
    let manager = ConfigManager::new(app_name);
    let _manager_discovery = manager.discover();

    println!(
        "   Config path from manager: {}",
        manager.config_path().display()
    );
    println!(
        "   Config file path: {}",
        manager.config_file("config.toml").display()
    );

    // 3. Finding config files by pattern
    println!("\n3. Finding Config Files by Pattern:");
    let pattern = FilePattern::extensions("config", &["toml", "yaml", "json"]);
    let configs = manager.find_config_files(&pattern);

    println!("   Found {} config candidates:", configs.len());
    for config in &configs {
        if config.exists() {
            println!("     [FOUND] {} ({:?})", config.path.display(), config.tier);
        }
    }

    // 4. Finding conf.d fragments
    println!("\n4. Finding Configuration Fragments:");
    let fragments = manager.find_fragments(&FilePattern::glob("*.conf"), "conf.d");

    if fragments.is_empty() {
        println!("   No conf.d fragments found");
    } else {
        println!("   Found {} fragments:", fragments.len());
        for fragment in &fragments {
            println!("     - {}", fragment.display());
        }
    }

    // 5. Custom discovery options
    println!("\n5. Custom Discovery Options:");
    let options = DiscoveryOptions::new()
        .with_pattern(FilePattern::extensions("config", &["toml", "yaml"]))
        .with_fragments(true)
        .with_fragment_dir("conf.d");

    let custom_discovery = manager.discover_with_options(&options);
    println!(
        "   Custom pattern search found {} candidates",
        custom_discovery.candidates.len()
    );

    // 6. Using convenience functions
    println!("\n6. Convenience Functions:");
    println!(
        "   config_path('myapp') = {}",
        config_path("myapp").display()
    );
    println!(
        "   config_file_path('myapp', 'settings.toml') = {}",
        config_file_path("myapp", "settings.toml").display()
    );

    // 7. Summary
    println!("\n=== Summary ===");
    println!("Total locations searched: {}", discovery.candidates.len());
    println!(
        "Existing locations: {}",
        discovery.candidates.iter().filter(|c| c.exists()).count()
    );
    println!(
        "Configuration fragments found: {}",
        discovery.fragments.len()
    );
}
