use mimirs_agent::MimirAgent;
use mimirs_core::{MemoryAgent, MemoryInput, RecallQuery};
use mimirs_store::Urdarbrunnr;
use std::sync::Arc;
use tempfile::tempdir;

#[tokio::test]
async fn test_three_node_cluster_simulation() {
    // Simulate 3 regions
    let dir_us_east = tempdir().unwrap();
    let dir_us_west = tempdir().unwrap();
    let dir_eu_central = tempdir().unwrap();

    let well_east = Arc::new(Urdarbrunnr::opna(dir_us_east.path(), 384).unwrap());
    let well_west = Arc::new(Urdarbrunnr::opna(dir_us_west.path(), 384).unwrap());
    let well_eu = Arc::new(Urdarbrunnr::opna(dir_eu_central.path(), 384).unwrap());

    let agent_east = MimirAgent::new(well_east, mimirs_agent::AgentConfig::default());
    let agent_west = MimirAgent::new(well_west, mimirs_agent::AgentConfig::default());
    let agent_eu = MimirAgent::new(well_eu, mimirs_agent::AgentConfig::default());

    // US-EAST receives a memory
    let input = MemoryInput {
        content: "Global consensus knowledge".to_string(),
        metadata: None,
        verifiability: None,
        scope: None,
        memory_class: None,
        source_session: None,
    };

    agent_east.store(input.clone()).await.unwrap();

    // Verify it exists in EAST
    let results_east = agent_east.recall(RecallQuery::new("Global")).await.unwrap();
    assert!(!results_east.is_empty());

    // US-WEST and EU-CENTRAL should NOT have it yet (not synchronized)
    let results_west = agent_west.recall(RecallQuery::new("Global")).await.unwrap();
    assert!(results_west.is_empty());

    // Simulate synchronization (manual for Phase 9 simulation)
    agent_west.store(input.clone()).await.unwrap();
    agent_eu.store(input.clone()).await.unwrap();

    // Verify consistency across regions
    let results_west_after = agent_west.recall(RecallQuery::new("Global")).await.unwrap();
    assert!(!results_west_after.is_empty());

    let results_eu_after = agent_eu.recall(RecallQuery::new("Global")).await.unwrap();
    assert!(!results_eu_after.is_empty());
}
