//! Conditional and Required Fields Demonstration
//!
//! This example demonstrates:
//! 1. Required fields (marked with * - must be filled)
//! 2. Optional fields (can be skipped by pressing Enter)
//! 3. Conditional fields (only shown if condition is met)
//!
//! Run with: cargo run --example conditional_required_demo

use typedialog_core::form_parser;
use typedialog_core::prelude::*;

fn main() -> Result<()> {
    println!("╔════════════════════════════════════════════════════════════════╗");
    println!("║     Conditional & Required Fields Demonstration                ║");
    println!("╚════════════════════════════════════════════════════════════════╝\n");

    println!("📋 FEATURES:\n");
    println!("  ✓ Required fields (*) - Must have a value");
    println!("  ✓ Optional fields - Can be empty (press Enter)");
    println!("  ✓ Conditional fields - Only shown when conditions are met\n");

    println!("💡 TRY THIS:\n");
    println!("  1. Select 'Business' as account type");
    println!("  2. Notice: Business name field appears");
    println!("  3. Enable 2FA");
    println!("  4. Select '2FA method'");
    println!("  5. If you select 'SMS', phone number field appears\n");

    println!("─────────────────────────────────────────────────────────────────\n");

    // Load and execute the conditional form
    let form = form_parser::load_from_file("examples/conditional_form.toml")?;
    let results = form_parser::execute(form)?;

    // Display results
    println!("\n╔════════════════════════════════════════════════════════════════╗");
    println!("║                      Form Completed!                           ║");
    println!("╚════════════════════════════════════════════════════════════════╝\n");

    println!("📊 Submitted Values:\n");

    // Create a nice display of results
    for (key, value) in &results {
        match value {
            serde_json::Value::String(s) => {
                println!("  {} = \"{}\"", key, s);
            }
            serde_json::Value::Bool(b) => {
                let icon = if *b { "✓" } else { "✗" };
                println!("  {} = {} ({})", key, b, icon);
            }
            serde_json::Value::Array(arr) => {
                println!("  {} = [", key);
                for item in arr {
                    if let serde_json::Value::String(s) = item {
                        println!("    \"{}\"", s);
                    }
                }
                println!("  ]");
            }
            _ => {
                println!("  {} = {}", key, value);
            }
        }
    }

    println!("\n💡 Key Insights:\n");
    println!("  • Fields with * are required - form won't proceed without them");
    println!("  • Fields marked (optional) can be skipped - just press Enter");
    println!("  • Some fields only appeared because their 'when' condition was met");
    println!("  • Example: 'phone_number' only shows if 2fa_method == SMS\n");

    println!("📝 JSON Output:\n");
    let json = serde_json::to_string_pretty(&results)?;
    println!("{}\n", json);

    println!("✨ Conditional and Required Fields Working Perfectly!\n");

    Ok(())
}
