// Tests for mecha10_behavior_runtime::config

use mecha10_behavior_runtime::{BehaviorConfig, CompositionConfig, ParallelPolicyConfig};
use std::collections::HashMap;

#[test]
fn test_parse_simple_sequence() {
    let json = r#"{
        "name": "test",
        "root": {
            "type": "sequence",
            "children": []
        }
    }"#;

    let config = BehaviorConfig::from_json(json).unwrap();
    assert_eq!(config.name, "test");

    match config.root {
        CompositionConfig::Sequence { children, .. } => {
            assert_eq!(children.len(), 0);
        }
        _ => panic!("Expected sequence"),
    }
}

#[test]
fn test_parse_node_with_config_ref() {
    let json = r#"{
        "name": "test",
        "root": {
            "type": "node",
            "node": "my_behavior",
            "config_ref": "my_config"
        },
        "configs": {
            "my_config": {
                "speed": 1.5
            }
        }
    }"#;

    let config = BehaviorConfig::from_json(json).unwrap();

    match &config.root {
        CompositionConfig::Node { node, config_ref, .. } => {
            assert_eq!(node, "my_behavior");
            assert_eq!(config_ref, &Some("my_config".to_string()));
        }
        _ => panic!("Expected node"),
    }

    let node_config = config.get_config("my_config").unwrap();
    assert_eq!(node_config["speed"], 1.5);
}

#[test]
fn test_parse_parallel_with_policy() {
    let json = r#"{
        "name": "test",
        "root": {
            "type": "parallel",
            "policy": "require_one",
            "children": []
        }
    }"#;

    let config = BehaviorConfig::from_json(json).unwrap();

    match config.root {
        CompositionConfig::Parallel { policy, .. } => {
            assert!(matches!(policy, ParallelPolicyConfig::RequireOne));
        }
        _ => panic!("Expected parallel"),
    }
}

#[test]
fn test_generate_schema() {
    let schema = BehaviorConfig::generate_schema().unwrap();
    assert!(schema.contains("BehaviorConfig"));
    assert!(schema.contains("CompositionConfig"));
}

#[test]
fn test_roundtrip() {
    let config = BehaviorConfig {
        schema: None,
        name: "test".to_string(),
        description: Some("Test behavior".to_string()),
        root: CompositionConfig::Sequence {
            children: vec![],
            name: None,
        },
        configs: HashMap::new(),
    };

    let json = config.to_json().unwrap();
    let parsed = BehaviorConfig::from_json(&json).unwrap();
    assert_eq!(parsed.name, config.name);
    assert_eq!(parsed.description, config.description);
}
