//! Tests for memory integration with Provider and AgentRunner.

use std::sync::Arc;

use anyhow::Result;
use uuid::Uuid;

use nenjo::Slug;
use nenjo::manifest::{
    AgentManifest, Manifest, ModelManifest, ProjectManifest, PromptConfig, PromptTemplates,
    model_manifest_slug,
};
use nenjo::memory::{MarkdownMemory, MemoryScope};
use nenjo::provider::{ModelProviderFactory, NoopToolFactory, Provider};
use nenjo::types::{AbilityPromptConfig, DomainPromptConfig};
use nenjo_models::traits::{ChatRequest, ChatResponse, ModelProvider, TokenUsage};

// ---------------------------------------------------------------------------
// Mock Provider
// ---------------------------------------------------------------------------

struct MockProvider {
    response_text: String,
}

impl MockProvider {
    fn new(text: &str) -> Self {
        Self {
            response_text: text.to_string(),
        }
    }
}

#[async_trait::async_trait]
impl ModelProvider for MockProvider {
    async fn chat(
        &self,
        _request: ChatRequest<'_>,
        _model: &str,
        _temperature: f64,
    ) -> Result<ChatResponse> {
        Ok(ChatResponse {
            text: Some(self.response_text.clone()),
            tool_calls: vec![],
            usage: TokenUsage {
                input_tokens: 100,
                output_tokens: 50,
            },
        })
    }

    fn context_window(&self, _model: &str) -> Option<usize> {
        Some(128_000)
    }
}

struct MockModelProviderFactory {
    response_text: String,
}

impl ModelProviderFactory for MockModelProviderFactory {
    fn create(&self, _provider_name: &str) -> Result<Arc<dyn ModelProvider>> {
        Ok(Arc::new(MockProvider::new(&self.response_text)))
    }
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn test_manifest() -> Manifest {
    let model = ModelManifest {
        id: Uuid::new_v4(),
        name: "test-model".into(),
        description: None,
        model: "mock-llm-v1".into(),
        model_provider: "mock".into(),
        temperature: Some(0.5),
        base_url: None,
    };

    let agent = AgentManifest {
        id: Uuid::new_v4(),
        name: "memory-agent".into(),
        slug: None,
        description: Some("An agent with memory".into()),
        prompt_config: PromptConfig {
            system_prompt: "You are a helpful assistant.\n{{ memories }}".into(),
            templates: PromptTemplates {
                task_execution: String::new(),
                chat_task: "{{ chat.message }}".into(),
                gate_eval: String::new(),
                heartbeat_task: String::new(),
            },
            ..Default::default()
        },
        color: None,
        model: Some(model_manifest_slug(&model.model_provider, &model.model)),
        domains: vec![],
        platform_scopes: vec![],
        mcp_servers: vec![],
        script_tools: vec![],
        abilities: vec![],
        prompt_locked: false,
        heartbeat: None,
    };

    let project = ProjectManifest {
        id: Uuid::new_v4(),
        name: "test-project".into(),
        slug: Slug::derive("test-project"),
        description: None,
        settings: serde_json::Value::Null,
    };

    Manifest {
        agents: vec![agent],
        models: vec![model],
        projects: vec![project],
        ..Default::default()
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[tokio::test]
async fn provider_with_memory_adds_tools() {
    let dir = tempfile::tempdir().unwrap();
    let ws_dir = tempfile::tempdir().unwrap();
    let memory = MarkdownMemory::new(dir.path(), ws_dir.path());

    let provider = Provider::builder()
        .with_manifest(test_manifest())
        .with_model_factory(MockModelProviderFactory {
            response_text: "ok".into(),
        })
        .with_tool_factory(NoopToolFactory)
        .with_memory(memory)
        .build()
        .await
        .unwrap();

    let runner = provider
        .agent("memory-agent")
        .await
        .unwrap()
        .build()
        .await
        .unwrap();

    let specs = runner.instance().tool_specs();
    let names: Vec<&str> = specs.iter().map(|s| s.name.as_str()).collect();

    assert!(names.contains(&"save_memory"), "should have save_memory");
    assert!(
        names.contains(&"recall_memory"),
        "should have recall_memory"
    );
    assert!(
        names.contains(&"forget_memory"),
        "should have forget_memory"
    );
    assert!(
        names.contains(&"save_artifact"),
        "should have save_artifact"
    );
    assert!(
        names.contains(&"read_artifact"),
        "should have read_artifact"
    );
    assert!(
        names.contains(&"delete_artifact"),
        "should have delete_artifact"
    );
}

#[tokio::test]
async fn provider_without_memory_has_no_memory_tools() {
    let provider = Provider::builder()
        .with_manifest(test_manifest())
        .with_model_factory(MockModelProviderFactory {
            response_text: "ok".into(),
        })
        .with_tool_factory(NoopToolFactory)
        .build()
        .await
        .unwrap();

    let runner = provider
        .agent("memory-agent")
        .await
        .unwrap()
        .build()
        .await
        .unwrap();

    let specs = runner.instance().tool_specs();
    let names: Vec<&str> = specs.iter().map(|s| s.name.as_str()).collect();
    assert!(!names.contains(&"save_memory"));
}

#[tokio::test]
async fn memory_store_and_recall() {
    let dir = tempfile::tempdir().unwrap();
    let ws_dir = tempfile::tempdir().unwrap();
    let memory = Arc::new(MarkdownMemory::new(dir.path(), ws_dir.path()));
    let scope = MemoryScope::new("test-agent", Some("test-project"));

    use nenjo::memory::Memory;

    // Store facts
    memory
        .append(&scope.project, "preferences", "User prefers Rust")
        .await
        .unwrap();
    memory
        .append(&scope.project, "preferences", "Always use snake_case")
        .await
        .unwrap();
    memory
        .append(&scope.core, "expertise", "Distributed systems")
        .await
        .unwrap();

    // Recall by category
    let cat = memory
        .read_category(&scope.project, "preferences")
        .await
        .unwrap()
        .unwrap();
    assert_eq!(cat.facts.len(), 2);
    assert_eq!(cat.facts[0].text, "User prefers Rust");

    // List all categories
    let cats = memory.list_categories(&scope.project).await.unwrap();
    assert_eq!(cats.len(), 1);
    assert_eq!(cats[0].category, "preferences");

    let core_cats = memory.list_categories(&scope.core).await.unwrap();
    assert_eq!(core_cats.len(), 1);
    assert_eq!(core_cats[0].category, "expertise");
}

#[tokio::test]
async fn memory_forget() {
    let dir = tempfile::tempdir().unwrap();
    let ws_dir = tempfile::tempdir().unwrap();
    let memory = Arc::new(MarkdownMemory::new(dir.path(), ws_dir.path()));
    let scope = MemoryScope::new("test-agent", Some("test-project"));

    use nenjo::memory::Memory;

    memory
        .append(&scope.project, "prefs", "Likes Rust")
        .await
        .unwrap();
    memory
        .append(&scope.project, "prefs", "Likes Go")
        .await
        .unwrap();

    assert!(
        memory
            .delete_fact(&scope.project, "prefs", "Likes Rust")
            .await
            .unwrap()
    );

    let cat = memory
        .read_category(&scope.project, "prefs")
        .await
        .unwrap()
        .unwrap();
    assert_eq!(cat.facts.len(), 1);
    assert_eq!(cat.facts[0].text, "Likes Go");
}

#[tokio::test]
async fn memory_vars_injected_into_prompts() {
    let dir = tempfile::tempdir().unwrap();
    let ws_dir = tempfile::tempdir().unwrap();
    let memory = Arc::new(MarkdownMemory::new(dir.path(), ws_dir.path()));
    let scope = MemoryScope::new("memory-agent", Some("test-project"));

    use nenjo::memory::Memory;

    // Store facts in each tier
    memory
        .append(&scope.core, "expertise", "Distributed systems expert")
        .await
        .unwrap();
    memory
        .append(&scope.project, "preferences", "User prefers Rust")
        .await
        .unwrap();
    memory
        .append(&scope.shared, "decisions", "Using PostgreSQL for DB")
        .await
        .unwrap();

    // Build memory vars
    let vars = nenjo::memory::build_memory_vars(memory.as_ref(), &scope)
        .await
        .unwrap();

    let full = vars.get("memories").expect("should have memories key");
    assert!(full.contains("<memories>"), "should have memories root tag");
    assert!(full.contains("<memories-core>"), "should have core tier");
    assert!(
        full.contains("<memories-project>"),
        "should have project tier"
    );
    assert!(
        full.contains("<memories-shared>"),
        "should have shared tier"
    );
    assert!(
        full.contains("User prefers Rust"),
        "should contain project fact"
    );
    assert!(
        full.contains("Distributed systems"),
        "should contain core fact"
    );
    assert!(full.contains("PostgreSQL"), "should contain shared fact");

    // Individual tiers
    assert!(vars.contains_key("memories.core"), "should have core key");
    assert!(
        vars.contains_key("memories.project"),
        "should have project key"
    );
    assert!(
        vars.contains_key("memories.shared"),
        "should have shared key"
    );
}

#[tokio::test]
async fn memory_vars_empty_when_no_facts() {
    let dir = tempfile::tempdir().unwrap();
    let ws_dir = tempfile::tempdir().unwrap();
    let memory = Arc::new(MarkdownMemory::new(dir.path(), ws_dir.path()));
    let scope = MemoryScope::new("empty-agent", Some("empty-project"));

    let vars = nenjo::memory::build_memory_vars(memory.as_ref(), &scope)
        .await
        .unwrap();

    assert!(vars.is_empty(), "should be empty when no facts exist");
}

#[tokio::test]
async fn artifact_vars_injected() {
    let dir = tempfile::tempdir().unwrap();
    let ws_dir = tempfile::tempdir().unwrap();
    let memory = Arc::new(MarkdownMemory::new(dir.path(), ws_dir.path()));
    let scope = MemoryScope::new("test-agent", Some("test-project"));

    use nenjo::memory::Memory;

    memory
        .save_artifact(
            &scope.artifacts_project,
            "auth-prd.md",
            "Auth PRD",
            "architect",
            "# Auth PRD\nOAuth2 flow",
        )
        .await
        .unwrap();
    memory
        .save_artifact(
            &scope.artifacts_global,
            "standards.md",
            "Coding standards",
            "system",
            "# Standards\nUse Rust",
        )
        .await
        .unwrap();

    let vars = nenjo::memory::build_artifact_vars(memory.as_ref(), &scope)
        .await
        .unwrap();

    assert!(vars.contains_key("artifacts"), "should have artifacts key");
    assert!(
        vars.contains_key("artifacts.project"),
        "should have project key"
    );
    assert!(
        vars.contains_key("artifacts.workspace"),
        "should have workspace key"
    );

    let full = &vars["artifacts"];
    assert!(full.contains("auth-prd.md"));
    assert!(full.contains("standards.md"));
    assert!(full.contains("architect"));
}

// ---------------------------------------------------------------------------
// Scope isolation tests
// ---------------------------------------------------------------------------

#[tokio::test]
async fn scope_project_agent_three_tiers_isolated() {
    let dir = tempfile::tempdir().unwrap();
    let ws_dir = tempfile::tempdir().unwrap();
    let memory = Arc::new(MarkdownMemory::new(dir.path(), ws_dir.path()));

    use nenjo::memory::Memory;

    let scope = MemoryScope::new("coder", Some("webapp"));

    // Store in each tier
    memory
        .append(&scope.project, "prefs", "project-only")
        .await
        .unwrap();
    memory
        .append(&scope.core, "prefs", "core-only")
        .await
        .unwrap();
    memory
        .append(&scope.shared, "prefs", "shared-only")
        .await
        .unwrap();

    // Build memory vars — all three tiers present
    let vars = nenjo::memory::build_memory_vars(memory.as_ref(), &scope)
        .await
        .unwrap();
    let full = &vars["memories"];

    assert!(full.contains("project-only"));
    assert!(full.contains("core-only"));
    assert!(full.contains("shared-only"));

    // Each tier is separate in vars
    assert!(vars["memories.project"].contains("project-only"));
    assert!(!vars["memories.project"].contains("core-only"));

    assert!(vars["memories.core"].contains("core-only"));
    assert!(!vars["memories.core"].contains("project-only"));

    assert!(vars["memories.shared"].contains("shared-only"));
    assert!(!vars["memories.shared"].contains("project-only"));
}

#[tokio::test]
async fn scope_system_agent_collapses_to_core() {
    let dir = tempfile::tempdir().unwrap();
    let ws_dir = tempfile::tempdir().unwrap();
    let memory = Arc::new(MarkdownMemory::new(dir.path(), ws_dir.path()));

    use nenjo::memory::Memory;

    let scope = MemoryScope::new("nenji", None);

    // Project and core both write to agent_nenji_core
    memory
        .append(&scope.project, "prefs", "fact-a")
        .await
        .unwrap();
    memory.append(&scope.core, "prefs", "fact-b").await.unwrap();

    let vars = nenjo::memory::build_memory_vars(memory.as_ref(), &scope)
        .await
        .unwrap();

    // Project tier is skipped when it resolves to the same namespace as core
    // (system agents with no project), so only core should appear.
    assert!(!vars.contains_key("memories.project"));

    let core_xml = &vars["memories.core"];
    assert!(core_xml.contains("fact-a"));
    assert!(core_xml.contains("fact-b"));
}

#[tokio::test]
async fn scope_shared_visible_across_agents() {
    let dir = tempfile::tempdir().unwrap();
    let ws_dir = tempfile::tempdir().unwrap();
    let memory = Arc::new(MarkdownMemory::new(dir.path(), ws_dir.path()));

    use nenjo::memory::Memory;

    let scope_coder = MemoryScope::new("coder", Some("webapp"));
    let scope_reviewer = MemoryScope::new("reviewer", Some("webapp"));

    // Coder stores a shared fact
    memory
        .append(&scope_coder.shared, "conventions", "Always write tests")
        .await
        .unwrap();

    // Reviewer can see it via their shared scope (same project)
    let vars = nenjo::memory::build_memory_vars(memory.as_ref(), &scope_reviewer)
        .await
        .unwrap();
    assert!(vars["memories.shared"].contains("Always write tests"));

    // But reviewer can't see coder's project-scoped memories
    let reviewer_project = memory
        .list_categories(&scope_reviewer.project)
        .await
        .unwrap();
    assert!(reviewer_project.is_empty());
}

#[tokio::test]
async fn scope_artifacts_shared_across_agents() {
    let dir = tempfile::tempdir().unwrap();
    let ws_dir = tempfile::tempdir().unwrap();
    let memory = Arc::new(MarkdownMemory::new(dir.path(), ws_dir.path()));

    use nenjo::memory::Memory;

    let scope_architect = MemoryScope::new("architect", Some("webapp"));
    let scope_coder = MemoryScope::new("coder", Some("webapp"));

    // Architect saves a project artifact
    memory
        .save_artifact(
            &scope_architect.artifacts_project,
            "design.md",
            "System design",
            "architect",
            "# Design doc",
        )
        .await
        .unwrap();

    // Coder can see it (same project artifacts path)
    let vars = nenjo::memory::build_artifact_vars(memory.as_ref(), &scope_coder)
        .await
        .unwrap();
    assert!(vars["artifacts.project"].contains("design.md"));
    assert!(vars["artifacts.project"].contains("architect"));

    // Coder can read the full content
    let content = memory
        .read_artifact(&scope_coder.artifacts_project, "design.md")
        .await
        .unwrap()
        .unwrap();
    assert!(content.contains("# Design doc"));
}

#[tokio::test]
async fn scope_artifacts_global_visible_to_all() {
    let dir = tempfile::tempdir().unwrap();
    let ws_dir = tempfile::tempdir().unwrap();
    let memory = Arc::new(MarkdownMemory::new(dir.path(), ws_dir.path()));

    use nenjo::memory::Memory;

    let scope_a = MemoryScope::new("agent-a", Some("project-x"));
    let scope_b = MemoryScope::new("agent-b", Some("project-y"));
    let scope_sys = MemoryScope::new("system-agent", None);

    // Agent A saves a global artifact
    memory
        .save_artifact(
            &scope_a.artifacts_global,
            "guide.md",
            "Onboarding guide",
            "agent-a",
            "# Guide",
        )
        .await
        .unwrap();

    // All agents see it regardless of project
    for scope in [&scope_a, &scope_b, &scope_sys] {
        let entries = memory
            .list_artifacts(&scope.artifacts_global)
            .await
            .unwrap();
        assert_eq!(entries.len(), 1, "global artifact should be visible");
        assert_eq!(entries[0].filename, "guide.md");
    }
}

// ---------------------------------------------------------------------------
// Ability & domain memory flow
// ---------------------------------------------------------------------------

#[tokio::test]
async fn ability_inherits_memory_vars() {
    use nenjo::manifest::AbilityManifest;

    let dir = tempfile::tempdir().unwrap();
    let ws_dir = tempfile::tempdir().unwrap();
    let memory = MarkdownMemory::new(dir.path(), ws_dir.path());

    use nenjo::memory::Memory;

    // Pre-populate memory so it shows up in vars
    let scope = MemoryScope::new("ability-agent", Some("test-project"));
    memory
        .append(&scope.core, "expertise", "Knows Rust deeply")
        .await
        .unwrap();

    let model = ModelManifest {
        id: Uuid::new_v4(),
        name: "test-model".into(),
        description: None,
        model: "mock-llm-v1".into(),
        model_provider: "mock".into(),
        temperature: Some(0.5),
        base_url: None,
    };

    let ability = AbilityManifest {
        id: Uuid::new_v4(),
        name: "code-review".into(),
        path: None,
        description: Some("Reviews code".into()),
        activation_condition: "when code review is needed".into(),
        prompt_config: AbilityPromptConfig {
            developer_prompt: "You review code.".into(),
        },
        platform_scopes: vec![],
        mcp_servers: vec![],
        script_tools: vec![],
        source_type: "native".into(),
        read_only: false,
        metadata: serde_json::Value::Null,
    };

    let agent = nenjo::manifest::AgentManifest {
        id: Uuid::new_v4(),
        name: "ability-agent".into(),
        slug: None,
        description: Some("Agent with abilities".into()),
        prompt_config: PromptConfig {
            system_prompt: "You are helpful.\n{{ memories }}".into(),
            templates: PromptTemplates {
                task_execution: String::new(),
                chat_task: "{{ chat.message }}".into(),
                gate_eval: String::new(),
                heartbeat_task: String::new(),
            },
            ..Default::default()
        },
        color: None,
        model: Some(model_manifest_slug(&model.model_provider, &model.model)),
        domains: vec![],
        platform_scopes: vec![],
        mcp_servers: vec![],
        script_tools: vec![],
        abilities: vec![ability.name.clone()],
        prompt_locked: false,
        heartbeat: None,
    };

    let project = nenjo::manifest::ProjectManifest {
        id: Uuid::new_v4(),
        name: "test-project".into(),
        slug: Slug::derive("test-project"),
        description: None,
        settings: serde_json::Value::Null,
    };

    let manifest = nenjo::manifest::Manifest {
        agents: vec![agent],
        models: vec![model],
        abilities: vec![ability],
        projects: vec![project],
        ..Default::default()
    };

    let provider = Provider::builder()
        .with_manifest(manifest)
        .with_model_factory(MockModelProviderFactory {
            response_text: "ok".into(),
        })
        .with_tool_factory(NoopToolFactory)
        .with_memory(memory)
        .build()
        .await
        .unwrap();

    let runner = provider
        .agent("ability-agent")
        .await
        .unwrap()
        .build()
        .await
        .unwrap();

    // The agent should have the ability broker tools, not a per-ability tool.
    let specs = runner.instance().tool_specs();
    let names: Vec<&str> = specs.iter().map(|s| s.name.as_str()).collect();
    assert!(
        names.contains(&"list_assigned_abilities"),
        "should have list_assigned_abilities"
    );
    assert!(names.contains(&"use_ability"), "should have use_ability");
    assert!(
        !names.contains(&"code_review"),
        "should not expose per-ability tool"
    );
    assert!(names.contains(&"save_memory"), "should have save_memory");

    // Memory vars should be empty on the instance (loaded at execution time)
    // but the memory backend is configured on the runner
    assert!(
        runner.memory().is_some(),
        "runner should have memory backend"
    );
}

#[tokio::test]
async fn domain_expansion_preserves_memory() {
    use nenjo::manifest::DomainManifest;

    let dir = tempfile::tempdir().unwrap();
    let ws_dir = tempfile::tempdir().unwrap();
    let memory = MarkdownMemory::new(dir.path(), ws_dir.path());

    use nenjo::memory::Memory;

    let scope = MemoryScope::new("domain-agent", Some("test-project"));
    memory
        .append(&scope.project, "decisions", "Using axum for HTTP")
        .await
        .unwrap();

    let model = ModelManifest {
        id: Uuid::new_v4(),
        name: "test-model".into(),
        description: None,
        model: "mock-llm-v1".into(),
        model_provider: "mock".into(),
        temperature: Some(0.5),
        base_url: None,
    };

    let domain = DomainManifest {
        id: Uuid::new_v4(),
        name: "prd".into(),
        path: String::new(),
        description: Some("Product requirements".into()),
        command: "/prd".into(),
        platform_scopes: vec![],
        abilities: vec![],
        mcp_servers: vec![],
        script_tools: vec![],
        prompt_config: DomainPromptConfig {
            developer_prompt_addon: Some("PRD mode".into()),
        },
    };

    let agent = nenjo::manifest::AgentManifest {
        id: Uuid::new_v4(),
        name: "domain-agent".into(),
        slug: None,
        description: Some("Agent with domains".into()),
        prompt_config: PromptConfig {
            system_prompt: "You are helpful.\n{{ memories }}".into(),
            templates: PromptTemplates {
                task_execution: String::new(),
                chat_task: "{{ chat.message }}".into(),
                gate_eval: String::new(),
                heartbeat_task: String::new(),
            },
            ..Default::default()
        },
        color: None,
        model: Some(model_manifest_slug(&model.model_provider, &model.model)),
        domains: vec![Slug::derive(&domain.name)],
        platform_scopes: vec![],
        mcp_servers: vec![],
        script_tools: vec![],
        abilities: vec![],
        prompt_locked: false,
        heartbeat: None,
    };

    let project = nenjo::manifest::ProjectManifest {
        id: Uuid::new_v4(),
        name: "test-project".into(),
        slug: Slug::derive("test-project"),
        description: None,
        settings: serde_json::Value::Null,
    };

    let manifest = nenjo::manifest::Manifest {
        agents: vec![agent],
        models: vec![model],
        domains: vec![domain],
        projects: vec![project],
        ..Default::default()
    };

    let provider = Provider::builder()
        .with_manifest(manifest)
        .with_model_factory(MockModelProviderFactory {
            response_text: "ok".into(),
        })
        .with_tool_factory(NoopToolFactory)
        .with_memory(memory)
        .build()
        .await
        .unwrap();

    let runner = provider
        .agent("domain-agent")
        .await
        .unwrap()
        .build()
        .await
        .unwrap();

    // Expand into domain
    let domain_runner = runner.domain_expansion("prd").await.unwrap();

    // Domain runner should preserve memory backend
    assert!(
        domain_runner.memory().is_some(),
        "domain runner should have memory backend"
    );
    assert!(
        domain_runner.memory_scope().is_some(),
        "domain runner should have memory scope"
    );

    // Memory tools should still be present
    let specs = domain_runner.instance().tool_specs();
    let names: Vec<&str> = specs.iter().map(|s| s.name.as_str()).collect();
    assert!(
        names.contains(&"save_memory"),
        "domain runner should have save_memory"
    );
    assert!(
        names.contains(&"save_artifact"),
        "domain runner should have save_artifact"
    );
}

// ---------------------------------------------------------------------------
// Runner execution
// ---------------------------------------------------------------------------

#[tokio::test]
async fn runner_with_memory_executes() {
    let dir = tempfile::tempdir().unwrap();
    let ws_dir = tempfile::tempdir().unwrap();
    let memory = MarkdownMemory::new(dir.path(), ws_dir.path());

    let provider = Provider::builder()
        .with_manifest(test_manifest())
        .with_model_factory(MockModelProviderFactory {
            response_text: "I see from memory this is a Rust project.".into(),
        })
        .with_tool_factory(NoopToolFactory)
        .with_memory(memory)
        .build()
        .await
        .unwrap();

    let runner = provider
        .agent("memory-agent")
        .await
        .unwrap()
        .build()
        .await
        .unwrap();

    let output = runner
        .chat("What do you know about this project?")
        .await
        .unwrap();
    assert_eq!(output.text, "I see from memory this is a Rust project.");
}
