//! MCP agent inspection tools — read logs, config, and resource usage.

use serde_json::{Value, json};

use crate::agents::PipelineStage;
use crate::config::ProjectConfig;
use crate::http::context::AppContext;

/// Read agent session logs from disk and return a human-readable timeline.
///
/// Stitches all session log files for the story together in chronological
/// order. If `agent_name` is omitted, logs from every agent are included.
/// Supports `lines` (tail the last N lines) and `filter` (substring match).
/// If a named agent is currently running, its buffered in-memory events are
/// included when no disk logs exist yet.
pub(crate) async fn tool_get_agent_output(
    args: &Value,
    ctx: &AppContext,
) -> Result<String, String> {
    use crate::agent_log;

    let story_id = args
        .get("story_id")
        .and_then(|v| v.as_str())
        .ok_or("Missing required argument: story_id")?;
    let agent_name_filter = args.get("agent_name").and_then(|v| v.as_str());
    let tail = args
        .get("lines")
        .and_then(|v| v.as_u64())
        .map(|n| n as usize);
    let filter = args.get("filter").and_then(|v| v.as_str());

    let project_root = ctx.services.agents.get_project_root(&ctx.state)?;

    // Collect all matching log files, oldest first.
    let log_files = agent_log::list_story_log_files(&project_root, story_id, agent_name_filter);

    let mut all_lines: Vec<String> = Vec::new();

    for path in &log_files {
        let file_name = path.file_name().and_then(|n| n.to_str()).unwrap_or("?");
        all_lines.push(format!("=== {} ===", file_name.trim_end_matches(".log")));
        match agent_log::read_log_as_readable_lines(path) {
            Ok(lines) => all_lines.extend(lines),
            Err(e) => all_lines.push(format!("[ERROR reading log: {e}]")),
        }
        all_lines.push(String::new()); // blank line between sessions
    }

    // Append buffered live events only when no disk logs exist yet.
    // emit_event() writes to disk synchronously, so disk is always up-to-date
    // when log files are present. Showing live events alongside disk logs would
    // produce duplicates. Only fall back to in-memory events when the log
    // writer failed and nothing was persisted to disk.
    if log_files.is_empty()
        && let Some(agent_name) = agent_name_filter
        && let Ok(live_events) = ctx.services.agents.drain_events(story_id, agent_name).await
        && !live_events.is_empty()
    {
        all_lines.push(format!("=== {agent_name} (live) ==="));
        let now = chrono::Utc::now().to_rfc3339();
        for event in &live_events {
            if let Ok(event_value) = serde_json::to_value(event)
                && let Some(line) = agent_log::format_log_entry_as_text(&now, &event_value)
            {
                all_lines.push(line);
            }
        }
        all_lines.push(String::new());
    }

    if log_files.is_empty() {
        let agent_hint = agent_name_filter
            .map(|n| format!(" agent '{n}'"))
            .unwrap_or_default();
        return Ok(format!(
            "No log files found for story '{story_id}'{agent_hint}."
        ));
    }

    // Apply substring filter (always keep section headers).
    let filtered: Vec<String> = if let Some(f) = filter {
        all_lines
            .into_iter()
            .filter(|l| l.starts_with("===") || l.contains(f))
            .collect()
    } else {
        all_lines
    };

    // Apply tail (last N lines).
    let output = if let Some(n) = tail {
        let start = filtered.len().saturating_sub(n);
        filtered[start..].join("\n")
    } else {
        filtered.join("\n")
    };

    Ok(output)
}

/// Return the agent configuration with availability info for each stage.
pub(crate) async fn tool_get_agent_config(ctx: &AppContext) -> Result<String, String> {
    let project_root = ctx.services.agents.get_project_root(&ctx.state)?;
    let config = ProjectConfig::load(&project_root)?;

    // Collect available (idle) agent names across all stages so the caller can
    // see at a glance which agents are free to start (story 190).
    let mut available_names: std::collections::HashSet<String> = std::collections::HashSet::new();
    for stage in &[
        PipelineStage::Coder,
        PipelineStage::Qa,
        PipelineStage::Mergemaster,
        PipelineStage::Other,
    ] {
        if let Ok(names) = ctx
            .services
            .agents
            .available_agents_for_stage(&config, stage)
            .await
        {
            available_names.extend(names);
        }
    }

    serde_json::to_string_pretty(&json!(
        config
            .agent
            .iter()
            .map(|a| json!({
                "name": a.name,
                "role": a.role,
                "model": a.model,
                "allowed_tools": a.allowed_tools,
                "max_turns": a.max_turns,
                "max_budget_usd": a.max_budget_usd,
                "available": available_names.contains(&a.name),
            }))
            .collect::<Vec<_>>()
    ))
    .map_err(|e| format!("Serialization error: {e}"))
}

/// Get remaining turns and budget for a running agent.
///
/// Returns turns used, max turns, remaining turns, budget used, max budget,
/// and remaining budget for the named agent. Fails if the agent is not
/// currently running or pending.
pub(crate) async fn tool_get_agent_remaining_turns_and_budget(
    args: &Value,
    ctx: &AppContext,
) -> Result<String, String> {
    let story_id = args
        .get("story_id")
        .and_then(|v| v.as_str())
        .ok_or("Missing required argument: story_id")?;
    let agent_name = args
        .get("agent_name")
        .and_then(|v| v.as_str())
        .ok_or("Missing required argument: agent_name")?;

    // Verify the agent exists and is running/pending.
    let agents = ctx.services.agents.list_agents().await?;
    let agent_info = agents
        .iter()
        .find(|a| a.story_id == story_id && a.agent_name == agent_name)
        .ok_or_else(|| format!("No agent '{agent_name}' found for story '{story_id}'"))?;

    if !matches!(
        agent_info.status,
        crate::agents::AgentStatus::Running | crate::agents::AgentStatus::Pending
    ) {
        let reason_suffix = agent_info
            .termination_reason
            .as_ref()
            .map(|r| {
                format!(
                    ", termination_reason: {}",
                    serde_json::to_string(r).unwrap_or_default()
                )
            })
            .unwrap_or_default();
        return Err(format!(
            "Agent '{agent_name}' for story '{story_id}' is not running (status: {}{reason_suffix})",
            agent_info.status
        ));
    }

    let project_root = ctx.services.agents.get_project_root(&ctx.state)?;
    let config = ProjectConfig::load(&project_root)?;

    // Find the agent config (max_turns, max_budget_usd).
    let agent_config = config.agent.iter().find(|a| a.name == agent_name);

    let max_turns = agent_config.and_then(|a| a.max_turns);
    let max_budget_usd = agent_config.and_then(|a| a.max_budget_usd);

    // ── Cumulative counters (all sessions) ─────────────────────────────
    let log_files =
        crate::agent_log::list_story_log_files(&project_root, story_id, Some(agent_name));
    let mut cumulative_turns: u64 = 0;
    for path in &log_files {
        if let Ok(entries) = crate::agent_log::read_log(path) {
            for entry in &entries {
                if entry.event.get("type").and_then(|v| v.as_str()) == Some("agent_json")
                    && let Some(data) = entry.event.get("data")
                    && data.get("type").and_then(|v| v.as_str()) == Some("assistant")
                {
                    cumulative_turns += 1;
                }
            }
        }
    }

    let cumulative_log_cost = crate::agents::pool::auto_assign::watchdog::compute_budget_from_logs(
        &project_root,
        story_id,
        agent_name,
    );
    let all_records = crate::agents::token_usage::read_all(&project_root).unwrap_or_default();
    let record_cost: f64 = all_records
        .iter()
        .filter(|r| r.story_id == story_id && r.agent_name == agent_name)
        .map(|r| r.usage.total_cost_usd)
        .sum();
    let cumulative_budget: f64 = cumulative_log_cost.max(record_cost);

    // ── Per-session counters (current session only — enforcement basis) ──
    use crate::agents::pool::auto_assign::watchdog::{
        compute_budget_from_single_log, count_turns_in_log, resolve_session_log,
    };
    let session_log = resolve_session_log(
        &project_root,
        story_id,
        agent_name,
        &agent_info.log_session_id,
    );
    let session_turns: u64 = session_log
        .as_ref()
        .map(|p| count_turns_in_log(p))
        .unwrap_or(0);
    let session_budget: f64 = session_log
        .as_ref()
        .map(|p| compute_budget_from_single_log(p))
        .unwrap_or(0.0);

    let remaining_turns = max_turns.map(|max| (max as i64) - (session_turns as i64));
    let remaining_budget_usd = max_budget_usd.map(|max| max - session_budget);

    serde_json::to_string_pretty(&json!({
        "story_id": story_id,
        "agent_name": agent_name,
        "status": agent_info.status.to_string(),
        // Per-session values (watchdog enforcement basis):
        "turns_used": session_turns,
        "budget_used_usd": session_budget,
        // Cumulative values (all sessions, useful for cost analysis):
        "cumulative_turns_used": cumulative_turns,
        "cumulative_budget_used_usd": cumulative_budget,
        // Limits and remaining (computed from per-session values):
        "max_turns": max_turns,
        "remaining_turns": remaining_turns,
        "max_budget_usd": max_budget_usd,
        "remaining_budget_usd": remaining_budget_usd,
    }))
    .map_err(|e| format!("Serialization error: {e}"))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::http::test_helpers::test_ctx;
    use serde_json::json;

    #[tokio::test]
    async fn tool_get_agent_config_no_project_toml_returns_default_agent() {
        let tmp = tempfile::tempdir().unwrap();
        let ctx = test_ctx(tmp.path());
        // No project.toml → default config with one fallback agent
        let result = tool_get_agent_config(&ctx).await.unwrap();
        let parsed: Vec<Value> = serde_json::from_str(&result).unwrap();
        // Default config contains one agent entry with default values
        assert_eq!(
            parsed.len(),
            1,
            "default config should have one fallback agent"
        );
        assert!(parsed[0].get("name").is_some());
        assert!(parsed[0].get("role").is_some());
    }

    #[tokio::test]
    async fn tool_get_agent_output_missing_story_id() {
        let tmp = tempfile::tempdir().unwrap();
        let ctx = test_ctx(tmp.path());
        let result = tool_get_agent_output(&json!({}), &ctx).await;
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("story_id"));
    }

    #[tokio::test]
    async fn tool_get_agent_output_no_logs_returns_not_found_message() {
        let tmp = tempfile::tempdir().unwrap();
        let ctx = test_ctx(tmp.path());
        // No agent registered, no log file → returns "no log files found" message
        let result =
            tool_get_agent_output(&json!({"story_id": "99_nope", "agent_name": "bot"}), &ctx)
                .await
                .unwrap();
        assert!(
            result.contains("No log files found"),
            "expected 'No log files found' message: {result}"
        );
    }

    #[tokio::test]
    async fn tool_get_agent_output_agent_name_is_optional() {
        let tmp = tempfile::tempdir().unwrap();
        let ctx = test_ctx(tmp.path());
        // No agent_name provided — should succeed (no error)
        let result = tool_get_agent_output(&json!({"story_id": "99_nope"}), &ctx)
            .await
            .unwrap();
        assert!(result.contains("No log files found"));
    }

    #[tokio::test]
    async fn tool_get_agent_output_reads_from_disk() {
        use crate::agent_log::AgentLogWriter;
        use crate::agents::AgentEvent;
        use crate::store::StoreOps;

        let tmp = tempfile::tempdir().unwrap();
        let ctx = test_ctx(tmp.path());
        // Point the store at the tmp root so the tool can find log files.
        ctx.store
            .set("project_root", json!(tmp.path().to_string_lossy().as_ref()));

        // Write a log file
        let mut writer =
            AgentLogWriter::new(tmp.path(), "42_story_foo", "coder-1", "sess-test").unwrap();
        writer
            .write_event(&AgentEvent::Output {
                story_id: "42_story_foo".to_string(),
                agent_name: "coder-1".to_string(),
                text: "My readable output line".to_string(),
            })
            .unwrap();
        writer
            .write_event(&AgentEvent::Done {
                story_id: "42_story_foo".to_string(),
                agent_name: "coder-1".to_string(),
                session_id: Some("sess-test".to_string()),
            })
            .unwrap();
        drop(writer);

        let result = tool_get_agent_output(
            &json!({"story_id": "42_story_foo", "agent_name": "coder-1"}),
            &ctx,
        )
        .await
        .unwrap();

        assert!(
            result.contains("My readable output line"),
            "expected output text in result: {result}"
        );
        assert!(result.contains("DONE"), "expected DONE marker: {result}");
    }

    #[tokio::test]
    async fn tool_get_agent_output_tail_limits_lines() {
        use crate::agent_log::AgentLogWriter;
        use crate::agents::AgentEvent;
        use crate::store::StoreOps;

        let tmp = tempfile::tempdir().unwrap();
        let ctx = test_ctx(tmp.path());
        ctx.store
            .set("project_root", json!(tmp.path().to_string_lossy().as_ref()));

        let mut writer =
            AgentLogWriter::new(tmp.path(), "42_story_bar", "coder-1", "sess-tail").unwrap();
        for i in 0..10 {
            writer
                .write_event(&AgentEvent::Output {
                    story_id: "42_story_bar".to_string(),
                    agent_name: "coder-1".to_string(),
                    text: format!("line {i}"),
                })
                .unwrap();
        }
        drop(writer);

        let result = tool_get_agent_output(
            &json!({"story_id": "42_story_bar", "agent_name": "coder-1", "lines": 3}),
            &ctx,
        )
        .await
        .unwrap();

        // Should contain "line 7", "line 8", "line 9" but NOT "line 0"
        assert!(
            result.contains("line 9"),
            "should contain last line: {result}"
        );
        assert!(
            !result.contains("line 0"),
            "should not contain early lines: {result}"
        );
    }

    #[tokio::test]
    async fn tool_get_agent_output_filter_narrows_results() {
        use crate::agent_log::AgentLogWriter;
        use crate::agents::AgentEvent;
        use crate::store::StoreOps;

        let tmp = tempfile::tempdir().unwrap();
        let ctx = test_ctx(tmp.path());
        ctx.store
            .set("project_root", json!(tmp.path().to_string_lossy().as_ref()));

        let mut writer =
            AgentLogWriter::new(tmp.path(), "42_story_baz", "coder-1", "sess-filter").unwrap();
        writer
            .write_event(&AgentEvent::Output {
                story_id: "42_story_baz".to_string(),
                agent_name: "coder-1".to_string(),
                text: "needle line here".to_string(),
            })
            .unwrap();
        writer
            .write_event(&AgentEvent::Output {
                story_id: "42_story_baz".to_string(),
                agent_name: "coder-1".to_string(),
                text: "haystack only".to_string(),
            })
            .unwrap();
        drop(writer);

        let result = tool_get_agent_output(
            &json!({"story_id": "42_story_baz", "agent_name": "coder-1", "filter": "needle"}),
            &ctx,
        )
        .await
        .unwrap();

        assert!(
            result.contains("needle"),
            "filter should keep matching lines: {result}"
        );
        assert!(
            !result.contains("haystack"),
            "filter should remove non-matching lines: {result}"
        );
    }

    // ── get_agent_remaining_turns_and_budget tests ──────────────────────────

    #[tokio::test]
    async fn tool_get_agent_remaining_turns_and_budget_missing_story_id() {
        let tmp = tempfile::tempdir().unwrap();
        let ctx = test_ctx(tmp.path());
        let result =
            tool_get_agent_remaining_turns_and_budget(&json!({"agent_name": "coder-1"}), &ctx)
                .await;
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("story_id"));
    }

    #[tokio::test]
    async fn tool_get_agent_remaining_turns_and_budget_missing_agent_name() {
        let tmp = tempfile::tempdir().unwrap();
        let ctx = test_ctx(tmp.path());
        let result =
            tool_get_agent_remaining_turns_and_budget(&json!({"story_id": "1_test"}), &ctx).await;
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("agent_name"));
    }

    #[tokio::test]
    async fn tool_get_agent_remaining_turns_and_budget_no_agent_returns_error() {
        let tmp = tempfile::tempdir().unwrap();
        let ctx = test_ctx(tmp.path());
        let result = tool_get_agent_remaining_turns_and_budget(
            &json!({"story_id": "99_nope", "agent_name": "coder-1"}),
            &ctx,
        )
        .await;
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(
            err.contains("No agent"),
            "expected 'No agent' error, got: {err}"
        );
    }

    #[tokio::test]
    async fn tool_get_agent_remaining_turns_and_budget_completed_agent_returns_error() {
        use crate::agents::AgentStatus;
        let tmp = tempfile::tempdir().unwrap();
        let ctx = test_ctx(tmp.path());
        ctx.services
            .agents
            .inject_test_agent("42_story", "coder-1", AgentStatus::Completed);

        let result = tool_get_agent_remaining_turns_and_budget(
            &json!({"story_id": "42_story", "agent_name": "coder-1"}),
            &ctx,
        )
        .await;
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(
            err.contains("not running"),
            "expected 'not running' error, got: {err}"
        );
    }

    #[tokio::test]
    async fn tool_get_agent_remaining_turns_and_budget_running_agent_returns_data() {
        use crate::agents::AgentStatus;
        use crate::store::StoreOps;

        let tmp = tempfile::tempdir().unwrap();
        let ctx = test_ctx(tmp.path());
        ctx.store
            .set("project_root", json!(tmp.path().to_string_lossy().as_ref()));
        ctx.services
            .agents
            .inject_test_agent("42_story", "coder-1", AgentStatus::Running);

        let result = tool_get_agent_remaining_turns_and_budget(
            &json!({"story_id": "42_story", "agent_name": "coder-1"}),
            &ctx,
        )
        .await
        .unwrap();
        let parsed: Value = serde_json::from_str(&result).unwrap();

        assert_eq!(parsed["story_id"], "42_story");
        assert_eq!(parsed["agent_name"], "coder-1");
        assert_eq!(parsed["status"], "running");
        // Per-session values (enforcement basis).
        assert!(parsed.get("turns_used").is_some());
        assert!(parsed.get("budget_used_usd").is_some());
        // Cumulative values (all sessions, for cost analysis).
        assert!(parsed.get("cumulative_turns_used").is_some());
        assert!(parsed.get("cumulative_budget_used_usd").is_some());
        // Limits and remaining.
        assert!(parsed.get("max_turns").is_some());
        assert!(parsed.get("remaining_turns").is_some());
        assert!(parsed.get("max_budget_usd").is_some());
        assert!(parsed.get("remaining_budget_usd").is_some());
    }
}
