#![allow(clippy::expect_used, clippy::unwrap_used)]

use std::fs;
use std::sync::Arc;

use async_trait::async_trait;
use capo_agent::{AppBuilder, Config};
use motosan_agent_loop::{ChatOutput, LlmClient, LlmResponse, Message};
use motosan_agent_tool::ToolDef;
use tempfile::tempdir;

struct NoopClient;
#[async_trait]
impl LlmClient for NoopClient {
    async fn chat(
        &self,
        _messages: &[Message],
        _tools: &[ToolDef],
    ) -> motosan_agent_loop::Result<ChatOutput> {
        Ok(ChatOutput::new(LlmResponse::Message("ok".into())))
    }
}

#[tokio::test]
async fn load_all_returns_skills_when_present() {
    let agent_dir = tempdir().expect("tempdir");
    let cwd = tempdir().expect("tempdir");
    let global = agent_dir.path().join("skills");
    fs::create_dir_all(&global).expect("create_dir_all");
    fs::write(global.join("a.md"), "---\ndescription: d\n---\n").expect("write");

    let r = capo_agent::skills::load_all(cwd.path(), agent_dir.path());
    assert_eq!(r.skills.len(), 1);
}

#[tokio::test]
async fn caller_can_skip_load_all_for_no_skills_equivalent() {
    // Simulates `--no-skills`: skip the load entirely.
    // Contract: `with_skills(vec![])` builds cleanly and produces a system
    // prompt with no `<available_skills>` block (verified in skills_inject.rs).
    let mut config = Config::default();
    config.anthropic.api_key = Some("sk-unused-in-this-test".into());

    let app = AppBuilder::new()
        .with_config(config)
        .with_llm(Arc::new(NoopClient) as Arc<dyn LlmClient>)
        .with_builtin_tools()
        .disable_context_discovery()
        .with_skills(vec![])
        .build()
        .await
        .expect("build with empty skills");
    drop(app);
}
