//! Handler for the `depends` command.
//!
//! `{bot_name} depends <number> <dep1> [dep2 ...]` locates the work item by
//! number across all pipeline stages and writes (or updates) the `depends_on`
//! front-matter field with the provided dependency numbers.
//!
//! Passing no dependency numbers clears the field entirely.

use super::CommandContext;

/// Handle the `depends` command.
///
/// Syntax: `depends <number> [dep1 dep2 ...]`
///
/// - `depends 484 477 478` — set story 484's dependencies to [477, 478]
/// - `depends 484` — clear all dependencies for story 484
pub(super) fn handle_depends(ctx: &CommandContext) -> Option<String> {
    let args = ctx.args.trim();

    if args.is_empty() {
        return None;
    }

    let mut parts = args.split_whitespace();
    let num_str = parts.next().unwrap_or("");

    if num_str.is_empty() || !num_str.chars().all(|c| c.is_ascii_digit()) {
        return None;
    }

    // Parse dependency numbers.
    let mut deps: Vec<u32> = Vec::new();
    for token in parts {
        match token.parse::<u32>() {
            Ok(n) => deps.push(n),
            Err(_) => {
                return Some(format!(
                    "Invalid dependency number: `{token}`. All numbers must be positive integers."
                ));
            }
        }
    }

    // Find the story by numeric prefix: CRDT → content store → filesystem.
    let (story_id, _stage_dir, _path, _content) =
        match crate::chat::lookup::find_story_by_number(ctx.effective_root(), num_str) {
            Some(found) => found,
            None => {
                return Some(format!(
                    "No story, bug, or spike with number **{num_str}** found."
                ));
            }
        };

    // Story name comes from the CRDT register, not the on-disk YAML
    // (story 929 — CRDT is the sole source of story metadata).
    let story_name = crate::crdt_state::read_item(&story_id)
        .map(|w| w.name().to_string())
        .unwrap_or_else(|| story_id.clone());

    // Write depends_on to the typed CRDT register — single source of truth.
    // No YAML mutation: the CRDT register is the canonical location for deps.
    crate::crdt_state::set_depends_on(&story_id, &deps);

    if deps.is_empty() {
        Some(format!(
            "Cleared all dependencies for **{story_name}** ({story_id})."
        ))
    } else {
        let nums: Vec<String> = deps.iter().map(|n| n.to_string()).collect();
        Some(format!(
            "Set depends_on: [{}] for **{story_name}** ({story_id}).",
            nums.join(", ")
        ))
    }
}

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

#[cfg(test)]
mod tests {
    use super::super::{CommandDispatch, try_handle_command};

    fn depends_cmd_with_root(root: &std::path::Path, args: &str) -> Option<String> {
        let services = crate::services::Services::new_test(root.to_path_buf(), "Timmy".to_string());
        let room_id = "!test:example.com".to_string();
        let dispatch = CommandDispatch {
            services: &services,
            project_root: &services.project_root,
            bot_user_id: "@timmy:homeserver.local",
            room_id: &room_id,
        };
        try_handle_command(&dispatch, &format!("@timmy depends {args}"))
    }

    use crate::chat::test_helpers::write_story_file;

    #[test]
    fn depends_command_is_registered() {
        use super::super::commands;
        assert!(
            commands().iter().any(|c| c.name == "depends"),
            "depends command must be in the registry"
        );
    }

    #[test]
    fn depends_command_appears_in_help() {
        let result = super::super::tests::try_cmd_addressed(
            "Timmy",
            "@timmy:homeserver.local",
            "@timmy help",
        );
        let output = result.unwrap();
        assert!(
            output.contains("depends"),
            "help should list depends command: {output}"
        );
    }

    #[test]
    fn depends_no_args_routes_to_timmy() {
        let tmp = tempfile::TempDir::new().unwrap();
        let result = depends_cmd_with_root(tmp.path(), "");
        assert!(
            result.is_none(),
            "no args should route to LLM (None): {result:?}"
        );
    }

    #[test]
    fn depends_non_numeric_number_routes_to_timmy() {
        let tmp = tempfile::TempDir::new().unwrap();
        let result = depends_cmd_with_root(tmp.path(), "on 477 and 478");
        assert!(
            result.is_none(),
            "natural-language args should route to LLM (None): {result:?}"
        );
    }

    #[test]
    fn depends_well_formed_runs_handler() {
        let tmp = tempfile::TempDir::new().unwrap();
        let result = depends_cmd_with_root(tmp.path(), "999");
        assert!(
            result.is_some(),
            "well-formed numeric story number should run the command handler: {result:?}"
        );
    }

    #[test]
    fn depends_not_found_returns_error() {
        let tmp = tempfile::TempDir::new().unwrap();
        let output = depends_cmd_with_root(tmp.path(), "999 1 2").unwrap();
        assert!(
            output.contains("999") && output.contains("found"),
            "not-found should mention number: {output}"
        );
    }

    #[test]
    fn depends_invalid_dep_number_returns_error() {
        let tmp = tempfile::TempDir::new().unwrap();
        write_story_file(
            tmp.path(),
            "1_backlog",
            "9912_story_foo.md",
            "---\nname: Foo\n---\n",
            None,
        );
        let output = depends_cmd_with_root(tmp.path(), "9912 abc").unwrap();
        assert!(
            output.contains("Invalid dependency number"),
            "non-numeric dep should error: {output}"
        );
    }

    #[test]
    fn depends_sets_deps_in_crdt_not_yaml() {
        let tmp = tempfile::TempDir::new().unwrap();
        write_story_file(
            tmp.path(),
            "1_backlog",
            "9910_story_foo.md",
            "---\nname: Foo\n---\n",
            Some("Foo"),
        );
        let output = depends_cmd_with_root(tmp.path(), "9910 477 478").unwrap();
        assert!(
            output.contains("477") && output.contains("478"),
            "response should mention dep numbers: {output}"
        );
        // CRDT register must hold the deps.
        let view = crate::crdt_state::read_item("9910_story_foo").expect("CRDT should have story");
        assert_eq!(
            view.depends_on(),
            &[477, 478],
            "CRDT register should hold [477, 478]: {view:?}"
        );
        // Content store YAML must NOT be mutated with depends_on.
        let contents = crate::db::read_content(crate::db::ContentKey::Story("9910_story_foo"))
            .expect("content store should have story");
        assert!(
            !contents.contains("depends_on"),
            "content store YAML must not contain depends_on after chat command: {contents}"
        );
    }

    #[test]
    fn depends_clears_deps_in_crdt_not_yaml() {
        let tmp = tempfile::TempDir::new().unwrap();
        write_story_file(
            tmp.path(),
            "2_current",
            "9911_story_bar.md",
            "---\nname: Bar\n---\n",
            Some("Bar"),
        );
        // Pre-seed CRDT with deps so we can verify clearing.
        crate::crdt_state::set_depends_on("9911_story_bar", &[477]);
        let output = depends_cmd_with_root(tmp.path(), "9911").unwrap();
        assert!(
            output.contains("Cleared"),
            "should confirm clearing deps: {output}"
        );
        // CRDT register must be empty after clear.
        let view = crate::crdt_state::read_item("9911_story_bar").expect("CRDT should have story");
        assert!(
            view.depends_on().is_empty(),
            "CRDT register should be empty after clearing: {view:?}"
        );
        // Content store YAML must not be mutated.
        let contents = crate::db::read_content(crate::db::ContentKey::Story("9911_story_bar"))
            .expect("content store should have story");
        assert!(
            !contents.contains("depends_on"),
            "content store YAML must not contain depends_on after clear: {contents}"
        );
    }

    /// Regression (AC3, chat path): chat `depends` must write deps to CRDT only —
    /// no `depends_on` in the YAML content store.
    /// The MCP counterpart is `tool_update_story_depends_on_routes_to_crdt_not_yaml`
    /// in `http/mcp/story_tools/story/update.rs`. Both assert `Some(vec![500, 501])`
    /// proving identical CRDT state across transports.
    #[test]
    fn chat_depends_sets_crdt_no_yaml_regression() {
        let tmp = tempfile::TempDir::new().unwrap();
        write_story_file(
            tmp.path(),
            "1_backlog",
            "8790_story_chat_dep.md",
            "---\nname: Chat Dep\n---\n",
            Some("Chat Dep"),
        );

        let out = depends_cmd_with_root(tmp.path(), "8790 500 501").unwrap();
        assert!(
            out.contains("500"),
            "chat response should mention dep: {out}"
        );

        let view =
            crate::crdt_state::read_item("8790_story_chat_dep").expect("CRDT must have chat story");
        assert_eq!(
            view.depends_on(),
            &[500, 501],
            "CRDT must hold [500, 501]: {view:?}"
        );

        let content =
            crate::db::read_content(crate::db::ContentKey::Story("8790_story_chat_dep")).unwrap();
        assert!(
            !content.contains("depends_on"),
            "chat must not write depends_on to YAML: {content}"
        );
    }

    /// AC4 regression (chat path): set [1,2,3] → clear → replace [4,5].
    /// Verifies each write is reflected in CRDT and that replace does not append.
    #[test]
    fn depends_set_clear_replace_regression() {
        let tmp = tempfile::TempDir::new().unwrap();
        crate::crdt_state::init_for_test();
        write_story_file(
            tmp.path(),
            "1_backlog",
            "9920_story_scr.md",
            "---\nname: SCR\n---\n",
            Some("SCR"),
        );

        // Set to [1, 2, 3].
        let out = depends_cmd_with_root(tmp.path(), "9920 1 2 3").unwrap();
        assert!(out.contains("1"), "response should mention dep 1: {out}");
        let view = crate::crdt_state::read_item("9920_story_scr").expect("CRDT must have story");
        assert_eq!(
            view.depends_on(),
            &[1, 2, 3],
            "CRDT should hold [1,2,3]: {view:?}"
        );

        // Clear.
        let out = depends_cmd_with_root(tmp.path(), "9920").unwrap();
        assert!(out.contains("Cleared"), "clear should confirm: {out}");
        let view = crate::crdt_state::read_item("9920_story_scr").expect("CRDT must have story");
        assert!(
            view.depends_on().is_empty(),
            "CRDT should be empty after clear: {view:?}"
        );

        // Replace with [4, 5] — must not append to old list.
        let out = depends_cmd_with_root(tmp.path(), "9920 4 5").unwrap();
        assert!(out.contains("4"), "response should mention dep 4: {out}");
        let view = crate::crdt_state::read_item("9920_story_scr").expect("CRDT must have story");
        assert_eq!(
            view.depends_on(),
            &[4, 5],
            "CRDT should hold exactly [4,5] after replace: {view:?}"
        );
    }

    #[test]
    fn depends_finds_story_in_any_stage() {
        let tmp = tempfile::TempDir::new().unwrap();
        write_story_file(
            tmp.path(),
            "3_qa",
            "9913_story_inqa.md",
            "---\nname: In QA\n---\n",
            None,
        );
        let output = depends_cmd_with_root(tmp.path(), "9913 100").unwrap();
        assert!(
            output.contains("In QA") || output.contains("9913_story_inqa"),
            "should find story in qa stage: {output}"
        );
        assert!(output.contains("100"), "should mention dep 100: {output}");
    }
}
