//! create_story_file: write new story to CRDT/content store.

use super::super::create_item_in_backlog;

/// Write a new story file to the CRDT content store and return the generated story ID.
///
/// Routes through `create_item_in_backlog`, the single internal creation path.
/// Validates non-empty title and ≥ 1 acceptance criterion before writing anything.
pub fn create_story_file(
    root: &std::path::Path,
    name: &str,
    user_story: Option<&str>,
    description: Option<&str>,
    acceptance_criteria: &[String],
    depends_on: Option<&[u32]>,
    _commit: bool,
) -> Result<String, String> {
    let name_owned = name.to_string();
    let user_story_owned = user_story.map(str::to_string);
    let description_owned = description.map(str::to_string);
    let depends_on_owned: Option<Vec<u32>> = depends_on.map(<[u32]>::to_vec);
    let acs_owned: Vec<String> = acceptance_criteria.to_vec();

    create_item_in_backlog(
        root,
        "story",
        name,
        acceptance_criteria,
        depends_on,
        move |story_number| {
            let mut content = String::new();
            content.push_str("---\n");
            content.push_str("type: story\n");
            content.push_str(&format!("name: \"{}\"\n", name_owned.replace('"', "\\\"")));
            if let Some(ref deps) = depends_on_owned.filter(|d| !d.is_empty()) {
                let nums: Vec<String> = deps.iter().map(|n| n.to_string()).collect();
                content.push_str(&format!("depends_on: [{}]\n", nums.join(", ")));
            }
            content.push_str("---\n\n");
            content.push_str(&format!("# Story {story_number}: {name_owned}\n\n"));

            content.push_str("## User Story\n\n");
            if let Some(ref us) = user_story_owned {
                content.push_str(us);
                content.push('\n');
            } else {
                content.push_str("As a ..., I want ..., so that ...\n");
            }
            content.push('\n');

            if let Some(ref desc) = description_owned {
                content.push_str("## Description\n\n");
                content.push_str(desc);
                content.push('\n');
                content.push('\n');
            }

            content.push_str("## Acceptance Criteria\n\n");
            for criterion in &acs_owned {
                content.push_str(&format!("- [ ] {criterion}\n"));
            }
            content.push('\n');

            content.push_str("## Out of Scope\n\n");
            content.push_str("- TBD\n");
            content
        },
    )
}

/// Check off the Nth unchecked acceptance criterion in a story.
///
/// `criterion_index` is 0-based among unchecked (`- [ ]`) items.
#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;

    #[allow(dead_code)]
    fn setup_git_repo(root: &std::path::Path) {
        std::process::Command::new("git")
            .args(["init"])
            .current_dir(root)
            .output()
            .unwrap();
        std::process::Command::new("git")
            .args(["config", "user.email", "test@test.com"])
            .current_dir(root)
            .output()
            .unwrap();
        std::process::Command::new("git")
            .args(["config", "user.name", "Test"])
            .current_dir(root)
            .output()
            .unwrap();
        std::process::Command::new("git")
            .args(["commit", "--allow-empty", "-m", "init"])
            .current_dir(root)
            .output()
            .unwrap();
    }

    #[allow(dead_code)]
    fn story_with_criteria(n: usize) -> String {
        let mut s = "---\nname: Test Story\n---\n\n## Acceptance Criteria\n\n".to_string();
        for i in 0..n {
            s.push_str(&format!("- [ ] Criterion {i}\n"));
        }
        s
    }

    /// Helper to set up a story in the filesystem and content store for tests
    /// that use check/add criterion.
    #[allow(dead_code)]
    fn setup_story_in_fs(root: &std::path::Path, story_id: &str, content: &str) {
        let current = root.join(".huskies/work/2_current");
        fs::create_dir_all(&current).unwrap();
        fs::write(current.join(format!("{story_id}.md")), content).unwrap();
        // Also write to the global content store so read_story_content picks up this
        // content even when a previous test has left a stale entry for the same ID.
        crate::db::ensure_content_store();
        crate::db::write_content(crate::db::ContentKey::Story(story_id), content);
    }

    // --- create_story integration tests ---

    #[test]
    fn create_story_writes_correct_content() {
        crate::db::ensure_content_store();
        let tmp = tempfile::tempdir().unwrap();
        let backlog = tmp.path().join(".huskies/work/1_backlog");
        fs::create_dir_all(&backlog).unwrap();
        fs::write(backlog.join("36_story_existing.md"), "").unwrap();
        // Also write to content store so next_item_number sees it.
        crate::db::write_item_with_content(
            "36_story_existing",
            "1_backlog",
            "---\nname: Existing\n---\n",
            crate::db::ItemMeta::named("Existing"),
        );

        let number = super::super::super::next_item_number(tmp.path()).unwrap();
        // The number must be >= 37 (at least higher than the existing "36_story_existing.md"),
        // but the global content store may have higher-numbered items from parallel tests.
        assert!(number >= 37, "expected number >= 37, got: {number}");

        let slug = super::super::super::slugify_name("My New Feature");
        assert_eq!(slug, "my_new_feature");

        let filename = format!("{number}_{slug}.md");
        let filepath = backlog.join(&filename);

        let mut content = String::new();
        content.push_str("---\n");
        content.push_str("name: \"My New Feature\"\n");
        content.push_str("---\n\n");
        content.push_str(&format!("# Story {number}: My New Feature\n\n"));
        content.push_str("## User Story\n\n");
        content.push_str("As a dev, I want this feature\n\n");
        content.push_str("## Acceptance Criteria\n\n");
        content.push_str("- [ ] It works\n");
        content.push_str("- [ ] It is tested\n\n");
        content.push_str("## Out of Scope\n\n");
        content.push_str("- TBD\n");

        fs::write(&filepath, &content).unwrap();

        let written = fs::read_to_string(&filepath).unwrap();
        assert!(written.starts_with("---\n"));
        assert!(written.contains(&format!("# Story {number}: My New Feature")));
        assert!(written.contains("- [ ] It works"));
        assert!(written.contains("- [ ] It is tested"));
        assert!(written.contains("## Out of Scope"));
    }

    #[test]
    fn create_story_with_colon_in_name_persists_to_crdt() {
        crate::crdt_state::init_for_test();
        let tmp = tempfile::tempdir().unwrap();
        let name = "Server-owned agent completion: remove report_completion dependency";
        let acs = vec!["Completion handled server-side".to_string()];
        let result = create_story_file(tmp.path(), name, None, None, &acs, None, false);
        assert!(result.is_ok(), "create_story_file failed: {result:?}");

        let story_id = result.unwrap();
        let view =
            crate::crdt_state::read_item(&story_id).expect("CRDT entry should exist after create");
        assert_eq!(view.name(), name);
    }

    // ── check_criterion_in_file tests ─────────────────────────────────────────

    #[test]
    fn create_story_with_depends_on_persists_to_crdt() {
        crate::crdt_state::init_for_test();
        let tmp = tempfile::tempdir().unwrap();
        let acs = vec!["Dependent criterion".to_string()];
        let story_id = create_story_file(
            tmp.path(),
            "Dependent Story",
            None,
            None,
            &acs,
            Some(&[489]),
            false,
        )
        .unwrap();

        let view =
            crate::crdt_state::read_item(&story_id).expect("CRDT entry should exist after create");
        assert_eq!(view.depends_on(), &[489]);
    }

    // ── Story 730: numeric-only story IDs ─────────────────────────────────────

    #[test]
    fn create_story_file_returns_numeric_only_id() {
        crate::db::ensure_content_store();
        let tmp = tempfile::tempdir().unwrap();
        let acs = vec!["Feature works".to_string()];
        let result = create_story_file(tmp.path(), "My Feature", None, None, &acs, None, false);
        assert!(
            result.is_ok(),
            "create_story_file should succeed: {result:?}"
        );
        let story_id = result.unwrap();
        assert!(
            story_id.chars().all(|c| c.is_ascii_digit()),
            "story ID must be numeric-only, got: '{story_id}'"
        );
    }

    #[test]
    fn create_story_file_sets_item_type_register() {
        crate::crdt_state::init_for_test();
        crate::db::ensure_content_store();
        let tmp = tempfile::tempdir().unwrap();
        let acs = vec!["Type validated".to_string()];
        let story_id =
            create_story_file(tmp.path(), "Type Test Story", None, None, &acs, None, false)
                .unwrap();
        let view = crate::crdt_state::read_item(&story_id).expect("CRDT entry must exist");
        assert_eq!(
            view.item_type(),
            Some(crate::io::story_metadata::ItemType::Story),
            "CRDT register must be set to story"
        );
    }

    #[test]
    fn create_story_file_rejects_empty_title() {
        let tmp = tempfile::tempdir().unwrap();
        let acs = vec!["Some criterion".to_string()];
        let err = create_story_file(tmp.path(), "", None, None, &acs, None, false).unwrap_err();
        assert!(
            err.contains("empty") || err.contains("whitespace"),
            "error should mention empty/whitespace, got: {err}"
        );
    }

    #[test]
    fn create_story_file_rejects_whitespace_only_title() {
        let tmp = tempfile::tempdir().unwrap();
        let acs = vec!["Some criterion".to_string()];
        let err = create_story_file(tmp.path(), "   ", None, None, &acs, None, false).unwrap_err();
        assert!(
            err.contains("empty") || err.contains("whitespace"),
            "error should mention empty/whitespace, got: {err}"
        );
    }

    #[test]
    fn create_story_file_rejects_empty_acceptance_criteria() {
        let tmp = tempfile::tempdir().unwrap();
        let result = create_story_file(tmp.path(), "Valid Title", None, None, &[], None, false);
        assert!(result.is_err(), "empty ACs should be rejected");
        assert!(
            result.unwrap_err().contains("acceptance criterion"),
            "error should mention acceptance criterion"
        );
    }

    // ── Story 504: native JSON types in front_matter ───────────────────────────
}
