//! Dump a generic CEL context and demonstrate references/watchdog diffing.
//!
//! Usage: cargo run -p cel-context --example dump_context

use cel_context::{
    Bounds, ContentRole, ContextContribution, ContextElement, ContextMerger, ContextSource,
    ContextWatchdog, ElementState,
};

fn main() {
    let mut merger = ContextMerger::new().with_defaults("Example App", "Main");
    merger.push(ContextContribution::new(
        "test_source",
        vec![ContextElement {
            id: "button:submit".into(),
            label: Some("Submit".into()),
            description: None,
            element_type: "button".into(),
            value: None,
            bounds: Some(Bounds {
                x: 100,
                y: 200,
                width: 120,
                height: 32,
            }),
            state: ElementState {
                focused: true,
                ..Default::default()
            },
            parent_id: None,
            actions: Vec::new(),
            confidence: 0.0,
            source: ContextSource::NativeApi,
            content_role: ContentRole::Interactive,
            properties: Default::default(),
        }],
    ));

    let ctx = merger.build();
    println!("{}", serde_json::to_string_pretty(&ctx).unwrap());

    let reference = ctx.elements[0].to_reference(1920, 1080);
    let resolved = cel_context::resolve_reference(&ctx, &reference);
    println!(
        "resolved reference: {:?}",
        resolved.map(|el| el.id.as_str())
    );

    let mut watchdog = ContextWatchdog::new();
    assert!(watchdog.tick(&ctx, true).is_empty());

    let mut changed = ctx.clone();
    changed.elements[0].state.focused = false;
    let events = watchdog.tick(&changed, true);
    println!("watchdog events: {events:?}");
}
