use chrono::Duration;
use second_brain_core::{
    kuzu_store::KuzuStore,
    schema::{Memory, MemoryType},
    store::Store,
};

#[test]
fn new_memory_updated_at_equals_created_at() {
    let m = Memory::new(
        "test content".to_string(),
        MemoryType::Semantic,
        "test".to_string(),
        String::new(),
    );
    assert_eq!(m.updated_at, m.created_at);
}

#[test]
fn reinforce_does_not_change_updated_at() {
    let mut m = Memory::new(
        "test content".to_string(),
        MemoryType::Semantic,
        "test".to_string(),
        String::new(),
    );
    let original_updated_at = m.updated_at;
    let original_last_accessed = m.last_accessed;

    std::thread::sleep(std::time::Duration::from_millis(5));
    m.reinforce();

    assert_eq!(m.updated_at, original_updated_at);
    assert!(
        m.last_accessed > original_last_accessed,
        "last_accessed should advance after reinforce"
    );
}

#[test]
fn store_roundtrip_preserves_updated_at() {
    let store = KuzuStore::in_memory("test-machine".to_string()).unwrap();

    let mut m = Memory::new(
        "roundtrip test".to_string(),
        MemoryType::Semantic,
        "test".to_string(),
        String::new(),
    );
    // Set updated_at to a distinct value well before created_at so the test is
    // meaningful (we're verifying the stored value, not a default fallback).
    m.updated_at = m.created_at - Duration::seconds(60);
    let expected_updated_at = m.updated_at;

    store.store_memory(&m).unwrap();

    let retrieved = store
        .get_memory_with_embedding(m.id)
        .unwrap()
        .expect("memory should exist after store");

    assert_eq!(
        retrieved.updated_at, expected_updated_at,
        "updated_at must survive a store/retrieve roundtrip unchanged"
    );
}
