//! 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;
use crate::io::story_metadata::{
    parse_front_matter, write_depends_on, write_depends_on_in_content,
};

/// 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 Some(format!(
            "Usage: `{} depends <number> [dep1 dep2 ...]`\n\nExamples:\n\
             • `{0} depends 484 477 478` — set depends_on: [477, 478]\n\
             • `{0} depends 484` — clear all dependencies",
            ctx.services.bot_name
        ));
    }

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

    if !num_str.chars().all(|c| c.is_ascii_digit()) || num_str.is_empty() {
        return Some(format!(
            "Invalid story number: `{num_str}`. Usage: `{} depends <number> [dep1 dep2 ...]`",
            ctx.services.bot_name
        ));
    }

    // 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."
                ));
            }
        };

    let story_name = content
        .as_deref()
        .and_then(|c| parse_front_matter(c).ok())
        .and_then(|m| m.name)
        .unwrap_or_else(|| story_id.clone());

    // Prefer the CRDT content store; fall back to filesystem only when the
    // story has not been loaded into the DB (e.g. very early startup or tests
    // that haven't called write_item_with_content).
    if let Some(existing) = crate::db::read_content(&story_id) {
        let updated = write_depends_on_in_content(&existing, &deps);
        crate::db::write_content(&story_id, &updated);
        let stage = crate::pipeline_state::read_typed(&story_id)
            .ok()
            .flatten()
            .map(|i| i.stage.dir_name().to_string())
            .unwrap_or_else(|| stage_dir.clone());
        crate::db::write_item_with_content(&story_id, &stage, &updated);
        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(", ")
            ))
        }
    } else {
        match write_depends_on(&path, &deps) {
            Ok(()) if deps.is_empty() => Some(format!(
                "Cleared all dependencies for **{story_name}** ({story_id})."
            )),
            Ok(()) => {
                let nums: Vec<String> = deps.iter().map(|n| n.to_string()).collect();
                Some(format!(
                    "Set depends_on: [{}] for **{story_name}** ({story_id}).",
                    nums.join(", ")
                ))
            }
            Err(e) => Some(format!("Failed to update dependencies for {story_id}: {e}")),
        }
    }
}

// ---------------------------------------------------------------------------
// 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_returns_usage() {
        let tmp = tempfile::TempDir::new().unwrap();
        let output = depends_cmd_with_root(tmp.path(), "").unwrap();
        assert!(
            output.contains("Usage"),
            "no args should show usage: {output}"
        );
    }

    #[test]
    fn depends_non_numeric_number_returns_error() {
        let tmp = tempfile::TempDir::new().unwrap();
        let output = depends_cmd_with_root(tmp.path(), "abc 1 2").unwrap();
        assert!(
            output.contains("Invalid story number"),
            "non-numeric story number should error: {output}"
        );
    }

    #[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",
        );
        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_and_writes_to_content_store() {
        let tmp = tempfile::TempDir::new().unwrap();
        write_story_file(
            tmp.path(),
            "1_backlog",
            "9910_story_foo.md",
            "---\nname: Foo\n---\n",
        );
        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}"
        );
        let contents = crate::db::read_content("9910_story_foo")
            .expect("content store should have updated story");
        assert!(
            contents.contains("depends_on: [477, 478]"),
            "content store should have depends_on set: {contents}"
        );
    }

    #[test]
    fn depends_clears_deps_when_no_deps_given() {
        let tmp = tempfile::TempDir::new().unwrap();
        write_story_file(
            tmp.path(),
            "2_current",
            "9911_story_bar.md",
            "---\nname: Bar\ndepends_on: [477]\n---\n",
        );
        let output = depends_cmd_with_root(tmp.path(), "9911").unwrap();
        assert!(
            output.contains("Cleared"),
            "should confirm clearing deps: {output}"
        );
        let contents = crate::db::read_content("9911_story_bar")
            .expect("content store should have updated story");
        assert!(
            !contents.contains("depends_on"),
            "content store should have depends_on cleared: {contents}"
        );
    }

    #[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",
        );
        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}");
    }
}
