use crate::io::story_metadata::parse_front_matter;
use std::fs;
use std::path::Path;

use super::{next_item_number, slugify_name};

/// Create a bug file in `work/1_backlog/` with a deterministic filename and auto-commit.
///
/// Returns the bug_id (e.g. `"4_bug_login_crash"`).
pub fn create_bug_file(
    root: &Path,
    name: &str,
    description: &str,
    steps_to_reproduce: &str,
    actual_result: &str,
    expected_result: &str,
    acceptance_criteria: Option<&[String]>,
) -> Result<String, String> {
    let bug_number = next_item_number(root)?;
    let slug = slugify_name(name);

    if slug.is_empty() {
        return Err("Name must contain at least one alphanumeric character.".to_string());
    }

    let filename = format!("{bug_number}_bug_{slug}.md");
    let bugs_dir = root.join(".storkit").join("work").join("1_backlog");
    fs::create_dir_all(&bugs_dir)
        .map_err(|e| format!("Failed to create backlog directory: {e}"))?;

    let filepath = bugs_dir.join(&filename);
    if filepath.exists() {
        return Err(format!("Bug file already exists: {filename}"));
    }

    let bug_id = filepath
        .file_stem()
        .and_then(|s| s.to_str())
        .unwrap_or_default()
        .to_string();

    let mut content = String::new();
    content.push_str("---\n");
    content.push_str(&format!("name: \"{}\"\n", name.replace('"', "\\\"")));
    content.push_str("---\n\n");
    content.push_str(&format!("# Bug {bug_number}: {name}\n\n"));
    content.push_str("## Description\n\n");
    content.push_str(description);
    content.push_str("\n\n");
    content.push_str("## How to Reproduce\n\n");
    content.push_str(steps_to_reproduce);
    content.push_str("\n\n");
    content.push_str("## Actual Result\n\n");
    content.push_str(actual_result);
    content.push_str("\n\n");
    content.push_str("## Expected Result\n\n");
    content.push_str(expected_result);
    content.push_str("\n\n");
    content.push_str("## Acceptance Criteria\n\n");
    if let Some(criteria) = acceptance_criteria {
        for criterion in criteria {
            content.push_str(&format!("- [ ] {criterion}\n"));
        }
    } else {
        content.push_str("- [ ] Bug is fixed and verified\n");
    }

    fs::write(&filepath, &content).map_err(|e| format!("Failed to write bug file: {e}"))?;

    // Watcher handles the git commit asynchronously.

    Ok(bug_id)
}

/// Create a spike file in `work/1_backlog/` with a deterministic filename.
///
/// Returns the spike_id (e.g. `"4_spike_filesystem_watcher_architecture"`).
pub fn create_spike_file(
    root: &Path,
    name: &str,
    description: Option<&str>,
) -> Result<String, String> {
    let spike_number = next_item_number(root)?;
    let slug = slugify_name(name);

    if slug.is_empty() {
        return Err("Name must contain at least one alphanumeric character.".to_string());
    }

    let filename = format!("{spike_number}_spike_{slug}.md");
    let backlog_dir = root.join(".storkit").join("work").join("1_backlog");
    fs::create_dir_all(&backlog_dir)
        .map_err(|e| format!("Failed to create backlog directory: {e}"))?;

    let filepath = backlog_dir.join(&filename);
    if filepath.exists() {
        return Err(format!("Spike file already exists: {filename}"));
    }

    let spike_id = filepath
        .file_stem()
        .and_then(|s| s.to_str())
        .unwrap_or_default()
        .to_string();

    let mut content = String::new();
    content.push_str("---\n");
    content.push_str(&format!("name: \"{}\"\n", name.replace('"', "\\\"")));
    content.push_str("---\n\n");
    content.push_str(&format!("# Spike {spike_number}: {name}\n\n"));
    content.push_str("## Question\n\n");
    if let Some(desc) = description {
        content.push_str(desc);
        content.push('\n');
    } else {
        content.push_str("- TBD\n");
    }
    content.push('\n');
    content.push_str("## Hypothesis\n\n");
    content.push_str("- TBD\n\n");
    content.push_str("## Timebox\n\n");
    content.push_str("- TBD\n\n");
    content.push_str("## Investigation Plan\n\n");
    content.push_str("- TBD\n\n");
    content.push_str("## Findings\n\n");
    content.push_str("- TBD\n\n");
    content.push_str("## Recommendation\n\n");
    content.push_str("- TBD\n");

    fs::write(&filepath, &content).map_err(|e| format!("Failed to write spike file: {e}"))?;

    // Watcher handles the git commit asynchronously.

    Ok(spike_id)
}

/// Create a refactor work item file in `work/1_backlog/`.
///
/// Returns the refactor_id (e.g. `"5_refactor_split_agents_rs"`).
pub fn create_refactor_file(
    root: &Path,
    name: &str,
    description: Option<&str>,
    acceptance_criteria: Option<&[String]>,
) -> Result<String, String> {
    let refactor_number = next_item_number(root)?;
    let slug = slugify_name(name);

    if slug.is_empty() {
        return Err("Name must contain at least one alphanumeric character.".to_string());
    }

    let filename = format!("{refactor_number}_refactor_{slug}.md");
    let backlog_dir = root.join(".storkit").join("work").join("1_backlog");
    fs::create_dir_all(&backlog_dir)
        .map_err(|e| format!("Failed to create backlog directory: {e}"))?;

    let filepath = backlog_dir.join(&filename);
    if filepath.exists() {
        return Err(format!("Refactor file already exists: {filename}"));
    }

    let refactor_id = filepath
        .file_stem()
        .and_then(|s| s.to_str())
        .unwrap_or_default()
        .to_string();

    let mut content = String::new();
    content.push_str("---\n");
    content.push_str(&format!("name: \"{}\"\n", name.replace('"', "\\\"")));
    content.push_str("---\n\n");
    content.push_str(&format!("# Refactor {refactor_number}: {name}\n\n"));
    content.push_str("## Current State\n\n");
    content.push_str("- TBD\n\n");
    content.push_str("## Desired State\n\n");
    if let Some(desc) = description {
        content.push_str(desc);
        content.push('\n');
    } else {
        content.push_str("- TBD\n");
    }
    content.push('\n');
    content.push_str("## Acceptance Criteria\n\n");
    if let Some(criteria) = acceptance_criteria {
        for criterion in criteria {
            content.push_str(&format!("- [ ] {criterion}\n"));
        }
    } else {
        content.push_str("- [ ] Refactoring complete and all tests pass\n");
    }
    content.push('\n');
    content.push_str("## Out of Scope\n\n");
    content.push_str("- TBD\n");

    fs::write(&filepath, &content)
        .map_err(|e| format!("Failed to write refactor file: {e}"))?;

    // Watcher handles the git commit asynchronously.

    Ok(refactor_id)
}

/// Returns true if the item stem (filename without extension) is a bug item.
/// Bug items follow the pattern: {N}_bug_{slug}
fn is_bug_item(stem: &str) -> bool {
    // Format: {digits}_bug_{rest}
    let after_num = stem.trim_start_matches(|c: char| c.is_ascii_digit());
    after_num.starts_with("_bug_")
}

/// Extract the human-readable name from a bug file's first heading.
fn extract_bug_name(path: &Path) -> Option<String> {
    let contents = fs::read_to_string(path).ok()?;
    for line in contents.lines() {
        if let Some(rest) = line.strip_prefix("# Bug ") {
            // Format: "N: Name"
            if let Some(colon_pos) = rest.find(": ") {
                return Some(rest[colon_pos + 2..].to_string());
            }
        }
    }
    None
}

/// List all open bugs — files in `work/1_backlog/` matching the `_bug_` naming pattern.
///
/// Returns a sorted list of `(bug_id, name)` pairs.
pub fn list_bug_files(root: &Path) -> Result<Vec<(String, String)>, String> {
    let backlog_dir = root.join(".storkit").join("work").join("1_backlog");
    if !backlog_dir.exists() {
        return Ok(Vec::new());
    }

    let mut bugs = Vec::new();
    for entry in
        fs::read_dir(&backlog_dir).map_err(|e| format!("Failed to read backlog directory: {e}"))?
    {
        let entry = entry.map_err(|e| format!("Failed to read entry: {e}"))?;
        let path = entry.path();

        if path.is_dir() {
            continue;
        }

        if path.extension().and_then(|ext| ext.to_str()) != Some("md") {
            continue;
        }

        let stem = path
            .file_stem()
            .and_then(|s| s.to_str())
            .ok_or_else(|| "Invalid file name.".to_string())?;

        // Only include bug items: {N}_bug_{slug}
        if !is_bug_item(stem) {
            continue;
        }

        let bug_id = stem.to_string();
        let name = extract_bug_name(&path).unwrap_or_else(|| bug_id.clone());
        bugs.push((bug_id, name));
    }

    bugs.sort_by(|a, b| a.0.cmp(&b.0));
    Ok(bugs)
}

/// Returns true if the item stem (filename without extension) is a refactor item.
/// Refactor items follow the pattern: {N}_refactor_{slug}
fn is_refactor_item(stem: &str) -> bool {
    let after_num = stem.trim_start_matches(|c: char| c.is_ascii_digit());
    after_num.starts_with("_refactor_")
}

/// List all open refactors — files in `work/1_backlog/` matching the `_refactor_` naming pattern.
///
/// Returns a sorted list of `(refactor_id, name)` pairs.
pub fn list_refactor_files(root: &Path) -> Result<Vec<(String, String)>, String> {
    let backlog_dir = root.join(".storkit").join("work").join("1_backlog");
    if !backlog_dir.exists() {
        return Ok(Vec::new());
    }

    let mut refactors = Vec::new();
    for entry in fs::read_dir(&backlog_dir)
        .map_err(|e| format!("Failed to read backlog directory: {e}"))?
    {
        let entry = entry.map_err(|e| format!("Failed to read entry: {e}"))?;
        let path = entry.path();

        if path.is_dir() {
            continue;
        }

        if path.extension().and_then(|ext| ext.to_str()) != Some("md") {
            continue;
        }

        let stem = path
            .file_stem()
            .and_then(|s| s.to_str())
            .ok_or_else(|| "Invalid file name.".to_string())?;

        if !is_refactor_item(stem) {
            continue;
        }

        let refactor_id = stem.to_string();
        let name = fs::read_to_string(&path)
            .ok()
            .and_then(|contents| parse_front_matter(&contents).ok())
            .and_then(|m| m.name)
            .unwrap_or_else(|| refactor_id.clone());
        refactors.push((refactor_id, name));
    }

    refactors.sort_by(|a, b| a.0.cmp(&b.0));
    Ok(refactors)
}

#[cfg(test)]
mod tests {
    use super::*;

    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();
    }

    // ── Bug file helper tests ──────────────────────────────────────────────────

    #[test]
    fn next_item_number_starts_at_1_when_empty_bugs() {
        let tmp = tempfile::tempdir().unwrap();
        assert_eq!(super::super::next_item_number(tmp.path()).unwrap(), 1);
    }

    #[test]
    fn next_item_number_increments_from_existing_bugs() {
        let tmp = tempfile::tempdir().unwrap();
        let backlog = tmp.path().join(".storkit/work/1_backlog");
        fs::create_dir_all(&backlog).unwrap();
        fs::write(backlog.join("1_bug_crash.md"), "").unwrap();
        fs::write(backlog.join("3_bug_another.md"), "").unwrap();
        assert_eq!(super::super::next_item_number(tmp.path()).unwrap(), 4);
    }

    #[test]
    fn next_item_number_scans_archived_too() {
        let tmp = tempfile::tempdir().unwrap();
        let backlog = tmp.path().join(".storkit/work/1_backlog");
        let archived = tmp.path().join(".storkit/work/5_done");
        fs::create_dir_all(&backlog).unwrap();
        fs::create_dir_all(&archived).unwrap();
        fs::write(archived.join("5_bug_old.md"), "").unwrap();
        assert_eq!(super::super::next_item_number(tmp.path()).unwrap(), 6);
    }

    #[test]
    fn list_bug_files_empty_when_no_bugs_dir() {
        let tmp = tempfile::tempdir().unwrap();
        let result = list_bug_files(tmp.path()).unwrap();
        assert!(result.is_empty());
    }

    #[test]
    fn list_bug_files_excludes_archive_subdir() {
        let tmp = tempfile::tempdir().unwrap();
        let backlog_dir = tmp.path().join(".storkit/work/1_backlog");
        let archived_dir = tmp.path().join(".storkit/work/5_done");
        fs::create_dir_all(&backlog_dir).unwrap();
        fs::create_dir_all(&archived_dir).unwrap();
        fs::write(backlog_dir.join("1_bug_open.md"), "# Bug 1: Open Bug\n").unwrap();
        fs::write(archived_dir.join("2_bug_closed.md"), "# Bug 2: Closed Bug\n").unwrap();

        let result = list_bug_files(tmp.path()).unwrap();
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].0, "1_bug_open");
        assert_eq!(result[0].1, "Open Bug");
    }

    #[test]
    fn list_bug_files_sorted_by_id() {
        let tmp = tempfile::tempdir().unwrap();
        let backlog_dir = tmp.path().join(".storkit/work/1_backlog");
        fs::create_dir_all(&backlog_dir).unwrap();
        fs::write(backlog_dir.join("3_bug_third.md"), "# Bug 3: Third\n").unwrap();
        fs::write(backlog_dir.join("1_bug_first.md"), "# Bug 1: First\n").unwrap();
        fs::write(backlog_dir.join("2_bug_second.md"), "# Bug 2: Second\n").unwrap();

        let result = list_bug_files(tmp.path()).unwrap();
        assert_eq!(result.len(), 3);
        assert_eq!(result[0].0, "1_bug_first");
        assert_eq!(result[1].0, "2_bug_second");
        assert_eq!(result[2].0, "3_bug_third");
    }

    #[test]
    fn extract_bug_name_parses_heading() {
        let tmp = tempfile::tempdir().unwrap();
        let path = tmp.path().join("bug-1-crash.md");
        fs::write(&path, "# Bug 1: Login page crashes\n\n## Description\n").unwrap();
        let name = extract_bug_name(&path).unwrap();
        assert_eq!(name, "Login page crashes");
    }

    #[test]
    fn create_bug_file_writes_correct_content() {
        let tmp = tempfile::tempdir().unwrap();
        setup_git_repo(tmp.path());

        let bug_id = create_bug_file(
            tmp.path(),
            "Login Crash",
            "The login page crashes on submit.",
            "1. Go to /login\n2. Click submit",
            "Page crashes with 500 error",
            "Login succeeds",
            Some(&["Login form submits without error".to_string()]),
        )
        .unwrap();

        assert_eq!(bug_id, "1_bug_login_crash");

        let filepath = tmp
            .path()
            .join(".storkit/work/1_backlog/1_bug_login_crash.md");
        assert!(filepath.exists());
        let contents = fs::read_to_string(&filepath).unwrap();
        assert!(
            contents.starts_with("---\nname: \"Login Crash\"\n---"),
            "bug file must start with YAML front matter"
        );
        assert!(contents.contains("# Bug 1: Login Crash"));
        assert!(contents.contains("## Description"));
        assert!(contents.contains("The login page crashes on submit."));
        assert!(contents.contains("## How to Reproduce"));
        assert!(contents.contains("1. Go to /login"));
        assert!(contents.contains("## Actual Result"));
        assert!(contents.contains("Page crashes with 500 error"));
        assert!(contents.contains("## Expected Result"));
        assert!(contents.contains("Login succeeds"));
        assert!(contents.contains("## Acceptance Criteria"));
        assert!(contents.contains("- [ ] Login form submits without error"));
    }

    #[test]
    fn create_bug_file_rejects_empty_name() {
        let tmp = tempfile::tempdir().unwrap();
        let result = create_bug_file(tmp.path(), "!!!", "desc", "steps", "actual", "expected", None);
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("alphanumeric"));
    }

    #[test]
    fn create_bug_file_uses_default_acceptance_criterion() {
        let tmp = tempfile::tempdir().unwrap();
        setup_git_repo(tmp.path());

        create_bug_file(
            tmp.path(),
            "Some Bug",
            "desc",
            "steps",
            "actual",
            "expected",
            None,
        )
        .unwrap();

        let filepath = tmp.path().join(".storkit/work/1_backlog/1_bug_some_bug.md");
        let contents = fs::read_to_string(&filepath).unwrap();
        assert!(
            contents.starts_with("---\nname: \"Some Bug\"\n---"),
            "bug file must have YAML front matter"
        );
        assert!(contents.contains("- [ ] Bug is fixed and verified"));
    }

    // ── create_spike_file tests ────────────────────────────────────────────────

    #[test]
    fn create_spike_file_writes_correct_content() {
        let tmp = tempfile::tempdir().unwrap();

        let spike_id =
            create_spike_file(tmp.path(), "Filesystem Watcher Architecture", None).unwrap();

        assert_eq!(spike_id, "1_spike_filesystem_watcher_architecture");

        let filepath = tmp
            .path()
            .join(".storkit/work/1_backlog/1_spike_filesystem_watcher_architecture.md");
        assert!(filepath.exists());
        let contents = fs::read_to_string(&filepath).unwrap();
        assert!(
            contents.starts_with("---\nname: \"Filesystem Watcher Architecture\"\n---"),
            "spike file must start with YAML front matter"
        );
        assert!(contents.contains("# Spike 1: Filesystem Watcher Architecture"));
        assert!(contents.contains("## Question"));
        assert!(contents.contains("## Hypothesis"));
        assert!(contents.contains("## Timebox"));
        assert!(contents.contains("## Investigation Plan"));
        assert!(contents.contains("## Findings"));
        assert!(contents.contains("## Recommendation"));
    }

    #[test]
    fn create_spike_file_uses_description_when_provided() {
        let tmp = tempfile::tempdir().unwrap();
        let description = "What is the best approach for watching filesystem events?";

        create_spike_file(tmp.path(), "FS Watcher Spike", Some(description)).unwrap();

        let filepath =
            tmp.path().join(".storkit/work/1_backlog/1_spike_fs_watcher_spike.md");
        let contents = fs::read_to_string(&filepath).unwrap();
        assert!(contents.contains(description));
    }

    #[test]
    fn create_spike_file_uses_placeholder_when_no_description() {
        let tmp = tempfile::tempdir().unwrap();
        create_spike_file(tmp.path(), "My Spike", None).unwrap();

        let filepath = tmp.path().join(".storkit/work/1_backlog/1_spike_my_spike.md");
        let contents = fs::read_to_string(&filepath).unwrap();
        // Should have placeholder TBD in Question section
        assert!(contents.contains("## Question\n\n- TBD\n"));
    }

    #[test]
    fn create_spike_file_rejects_empty_name() {
        let tmp = tempfile::tempdir().unwrap();
        let result = create_spike_file(tmp.path(), "!!!", None);
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("alphanumeric"));
    }

    #[test]
    fn create_spike_file_with_special_chars_in_name_produces_valid_yaml() {
        let tmp = tempfile::tempdir().unwrap();
        let name = "Spike: compare \"fast\" vs slow encoders";
        let result = create_spike_file(tmp.path(), name, None);
        assert!(result.is_ok(), "create_spike_file failed: {result:?}");

        let backlog = tmp.path().join(".storkit/work/1_backlog");
        let spike_id = result.unwrap();
        let filename = format!("{spike_id}.md");
        let contents = fs::read_to_string(backlog.join(&filename)).unwrap();

        let meta = parse_front_matter(&contents).expect("front matter should be valid YAML");
        assert_eq!(meta.name.as_deref(), Some(name));
    }

    #[test]
    fn create_spike_file_increments_from_existing_items() {
        let tmp = tempfile::tempdir().unwrap();
        let backlog = tmp.path().join(".storkit/work/1_backlog");
        fs::create_dir_all(&backlog).unwrap();
        fs::write(backlog.join("5_story_existing.md"), "").unwrap();

        let spike_id = create_spike_file(tmp.path(), "My Spike", None).unwrap();
        assert!(spike_id.starts_with("6_spike_"), "expected spike number 6, got: {spike_id}");
    }
}
