//! Comprehensive autocompletion demonstration
//!
//! This example showcases all three autocompletion strategies:
//! 1. HistoryCompleter - Remembers previously entered values
//! 2. FilterCompleter - Dynamically filters available options
//! 3. PatternCompleter - Provides pattern-based suggestions
//!
//! Run with: cargo run --example autocompletion_demo

use typedialog_core::autocompletion::{FilterCompleter, HistoryCompleter, PatternCompleter, PatternType};

fn main() {
    println!("=== typedialog Autocompletion Demo ===\n");

    // ============================================================
    // 1. HISTORY-BASED AUTOCOMPLETION
    // ============================================================
    println!("1️⃣  HISTORY-BASED AUTOCOMPLETION");
    println!("   (Remembers previously entered emails)\n");

    let mut email_history = HistoryCompleter::new(5);

    // Simulate adding previous email entries
    let previous_emails = vec![
        "alice.johnson@company.com",
        "bob.smith@example.org",
        "charlie.brown@gmail.com",
        "diana.prince@outlook.com",
        "eve.wilson@enterprise.net",
    ];

    for email in &previous_emails {
        email_history.add(email.to_string());
    }

    println!("   Previous emails in history:");
    for (i, email) in email_history.get_history().iter().enumerate() {
        println!("     {}. {}", i + 1, email);
    }

    // Demonstrate suggestions
    println!("\n   Searching for 'alice':");
    let suggestions = email_history.suggest("alice");
    for suggestion in suggestions {
        println!("     → {}", suggestion);
    }

    println!("\n   Searching for '@company':");
    let suggestions = email_history.suggest("@company");
    for suggestion in suggestions {
        println!("     → {}", suggestion);
    }

    // Get first completion
    if let Some(first) = email_history.complete("charlie") {
        println!("\n   First match for 'charlie': {}", first);
    }

    // ============================================================
    // 2. FILTER-BASED AUTOCOMPLETION
    // ============================================================
    println!("\n\n2️⃣  FILTER-BASED AUTOCOMPLETION");
    println!("   (Dynamically filters available options)\n");

    let roles = vec![
        "Administrator".to_string(),
        "Developer".to_string(),
        "DevOps Engineer".to_string(),
        "Database Administrator".to_string(),
        "Security Analyst".to_string(),
        "Quality Assurance".to_string(),
        "Project Manager".to_string(),
        "User".to_string(),
        "Guest".to_string(),
    ];

    let role_completer = FilterCompleter::new(roles.clone());

    println!("   Available roles:");
    for (i, role) in roles.iter().enumerate() {
        println!("     {}. {}", i + 1, role);
    }

    // Filter by "admin"
    println!("\n   Filtering by 'admin':");
    let filtered = role_completer.filter("admin");
    for role in &filtered {
        println!("     → {}", role);
    }

    // Filter by "dev"
    println!("\n   Filtering by 'dev':");
    let filtered = role_completer.filter("dev");
    for role in &filtered {
        println!("     → {}", role);
    }

    // Get first matching
    if let Some(first) = role_completer.complete("sec") {
        println!("\n   First match for 'sec': {}", first);
    }

    // ============================================================
    // 3. PATTERN-BASED AUTOCOMPLETION
    // ============================================================
    println!("\n\n3️⃣  PATTERN-BASED AUTOCOMPLETION");
    println!("   (Provides intelligent suggestions)\n");

    // Email pattern
    println!("   📧 EMAIL PATTERN:");
    let mut email_completer = PatternCompleter::new(PatternType::Email);
    email_completer.add_suggestion("@mycompany.com".to_string());
    email_completer.add_suggestion("@internal.corp".to_string());

    println!("     Built-in suggestions:");
    for suggestion in email_completer.suggest("@") {
        println!("       → {}", suggestion);
    }

    println!("\n     Smart completion for 'john':");
    if let Some(completion) = email_completer.complete("john") {
        println!("       → john{}", completion);
    }

    // URL pattern
    println!("\n   🔗 URL PATTERN:");
    let url_completer = PatternCompleter::new(PatternType::Url);
    println!("     Available protocols:");
    for protocol in url_completer.suggest("") {
        println!("       → {}", protocol);
    }

    println!("\n     Completion for empty input:");
    if let Some(completion) = url_completer.complete("") {
        println!("       → {}", completion);
    }

    // File path pattern
    println!("\n   📁 FILE PATH PATTERN:");
    let path_completer = PatternCompleter::new(PatternType::FilePath);
    println!("     Available path prefixes:");
    for prefix in path_completer.suggest("") {
        println!("       → {}", prefix);
    }

    // IPv4 pattern
    println!("\n   🌐 IPv4 ADDRESS PATTERN:");
    let ipv4_completer = PatternCompleter::new(PatternType::IPv4);
    println!("     Common subnet suggestions:");
    for subnet in ipv4_completer.suggest("") {
        println!("       → {}", subnet);
    }

    println!("\n     Filtering by '192':");
    for subnet in ipv4_completer.suggest("192") {
        println!("       → {}", subnet);
    }

    // Port pattern
    println!("\n   🔌 PORT PATTERN:");
    let port_completer = PatternCompleter::new(PatternType::Port);
    println!("     Common ports:");
    for port in port_completer.suggest("") {
        println!("       → {}", port);
    }

    println!("\n     Smart completion for '8':");
    if let Some(completion) = port_completer.complete("8") {
        println!("       → localhost{}", completion);
    }

    // ============================================================
    // 4. COMBINED EXAMPLE - MULTI-FIELD FORM
    // ============================================================
    println!("\n\n4️⃣  COMBINED EXAMPLE - MULTI-FIELD FORM");
    println!("   (Using all three completers together)\n");

    struct UserForm {
        email: String,
        role: String,
        website: String,
        ip_address: String,
    }

    println!("   Simulating user registration form with autocompletion:\n");

    // Email field with history
    println!("   Field 1: Email (with history)");
    let mut email_hist = HistoryCompleter::new(3);
    email_hist.add("previous@example.com".to_string());
    email_hist.add("old@domain.com".to_string());
    println!("     Suggestions for 'prev': {:?}", email_hist.suggest("prev"));

    // Role field with filtering
    println!("\n   Field 2: Role (with filtering)");
    let roles = vec!["Admin".to_string(), "User".to_string(), "Moderator".to_string()];
    let role_filter = FilterCompleter::new(roles);
    println!("     Filtered options for 'ad': {:?}", role_filter.filter("ad"));

    // Website field with pattern
    println!("\n   Field 3: Website (with URL pattern)");
    let url_pattern = PatternCompleter::new(PatternType::Url);
    println!("     Suggestions for empty: {:?}", url_pattern.suggest(""));
    println!("     Smart completion: {:?}", url_pattern.complete(""));

    // IP field with pattern
    println!("\n   Field 4: IP Address (with IPv4 pattern)");
    let ip_pattern = PatternCompleter::new(PatternType::IPv4);
    println!("     Common networks: {:?}", ip_pattern.suggest(""));
    println!("     Completion for '10': {:?}", ip_pattern.complete("10"));

    // ============================================================
    // 5. ADVANCED USAGE - CHAINED COMPLETIONS
    // ============================================================
    println!("\n\n5️⃣  ADVANCED USAGE - CHAINED COMPLETIONS\n");

    println!("   Building auto-suggestion pipeline:");

    // Step 1: User types email prefix
    let typed = "user";
    println!("     User types: '{}' (for email)", typed);

    // Step 2: Try pattern-based completion first
    let mut email_pat = PatternCompleter::new(PatternType::Email);
    email_pat.add_suggestion("@work.com".to_string());
    if let Some(completion) = email_pat.complete(typed) {
        println!("     Pattern suggests: {}{}", typed, completion);
    }

    // Step 3: Fall back to history
    let mut hist = HistoryCompleter::new(10);
    hist.add("user.account@company.com".to_string());
    hist.add("username@domain.org".to_string());
    let history_matches = hist.suggest(typed);
    if !history_matches.is_empty() {
        println!("     History has {} matches: {:?}", history_matches.len(), history_matches);
    }

    // ============================================================
    // Summary
    // ============================================================
    println!("\n\n=== Summary ===");
    println!("✓ HistoryCompleter: Remembers & suggests previous values");
    println!("✓ FilterCompleter: Dynamically filters available options");
    println!("✓ PatternCompleter: Smart suggestions for common patterns");
    println!("\nAll three can be combined for powerful autocompletion!");
}
