//! Subscription builder example.
//!
//! Demonstrates:
//! - Using `SubscriptionBuilder` for flexible configuration
//! - Different subscription kinds
//! - Subscription options

use cfgmatic_reactive::prelude::*;
use std::time::Duration;

fn main() -> Result<()> {
    // Create a file subscription using builder
    let file_sub = Subscription::builder()
        .id("app-config")
        .file("/etc/myapp/config.toml")
        .debounce_ms(200)
        .priority(10)
        .active(true)
        .build();

    println!("File subscription:");
    println!("  ID: {}", file_sub.id);
    println!("  Kind: {:?}", file_sub.kind);
    println!("  Active: {}", file_sub.active);
    println!("  Debounce: {:?}", file_sub.debounce);

    // Create a directory subscription
    let dir_sub = Subscription::builder()
        .id("config-dir")
        .directory("/etc/myapp/config.d")
        .debounce(Duration::from_millis(500))
        .max_batch_size(50)
        .build();

    println!("\nDirectory subscription:");
    println!("  ID: {}", dir_sub.id);
    println!("  Kind: {:?}", dir_sub.kind);
    println!("  Max batch size: {}", dir_sub.max_batch_size);

    // Create a file subscription with recursive watching
    let recursive_sub = Subscription::builder()
        .id("recursive-config")
        .file_recursive("/etc/myapp")
        .handler_id("config-handler")
        .priority(5)
        .build();

    println!("\nRecursive file subscription:");
    println!("  ID: {}", recursive_sub.id);
    println!("  Handler ID: {}", recursive_sub.handler_id);
    println!("  Priority: {}", recursive_sub.priority);

    // Create an environment variable subscription
    let env_sub = Subscription::builder()
        .id("env-config")
        .env("MYAPP_CONFIG_PATH")
        .build();

    println!("\nEnvironment subscription:");
    println!("  ID: {}", env_sub.id);
    println!("  Kind: {:?}", env_sub.kind);

    // Using the dispatcher with subscriptions
    let dispatcher = Dispatcher::new();
    dispatcher.subscribe(file_sub)?;
    dispatcher.subscribe(dir_sub)?;
    dispatcher.subscribe(recursive_sub)?;
    dispatcher.subscribe(env_sub)?;

    println!("\nTotal subscriptions: {}", dispatcher.subscription_count());

    // Activate/deactivate subscriptions
    let sub = Subscription::default().deactivate();
    println!("\nDefault subscription active: {}", sub.active);

    let sub = sub.activate();
    println!("After activate: {}", sub.active);

    Ok(())
}
