use std::path::{Path, PathBuf};

use sgclaw::compat::config_adapter::build_zeroclaw_config_from_settings;
use sgclaw::config::DeepSeekSettings;
use zeroclaw::memory::MemoryCategory;

fn workspace_root(label: &str) -> PathBuf {
    let root = std::env::temp_dir().join(format!("{label}-{}", uuid::Uuid::new_v4()));
    std::fs::create_dir_all(&root).unwrap();
    root
}

#[tokio::test]
async fn compat_memory_adapter_uses_workspace_local_sqlite_backend() {
    let settings = DeepSeekSettings {
        api_key: "key".to_string(),
        base_url: "https://api.deepseek.com".to_string(),
        model: "deepseek-chat".to_string(),
        skills_dir: None,
    };
    let workspace_root = workspace_root("sgclaw-memory");
    let config = build_zeroclaw_config_from_settings(Path::new(&workspace_root), &settings);

    assert_eq!(config.memory.backend, "sqlite");
    assert_eq!(config.memory.embedding_provider, "none");
    assert!(!config.memory.response_cache_enabled);
    assert!(!config.memory.snapshot_enabled);
    assert!(config.storage.provider.config.provider.is_empty());

    let memory = sgclaw::compat::memory_adapter::build_memory(&config).unwrap();
    memory
        .store(
            "weather",
            "remember today's weather workflow",
            MemoryCategory::Conversation,
            None,
        )
        .await
        .unwrap();

    assert_eq!(memory.count().await.unwrap(), 1);
    assert!(sgclaw::compat::memory_adapter::brain_db_path(&config.workspace_dir).exists());
}
