//! Handler for the `coverage` command — show or refresh test coverage.
//!
//! Default (no args): reads the cached `.coverage_report.json` file (written by
//! `script/test_coverage`) and reports the overall percentage plus the top 5
//! lowest-covered files as improvement targets.
//!
//! `coverage run`: executes `script/test_coverage` to collect fresh coverage
//! data (which writes `.coverage_report.json`), then reports the result.

use serde::Deserialize;

use super::CommandContext;

const COVERAGE_REPORT_FILE: &str = ".coverage_report.json";
const BASELINE_FILE: &str = ".coverage_baseline";
const COVERAGE_SCRIPT: &str = "script/test_coverage";

/// Top-level structure of `.coverage_report.json`.
#[derive(Deserialize)]
struct CoverageReport {
    overall: f64,
    threshold: f64,
    files: Vec<FileCoverage>,
}

/// Per-file coverage entry.
#[derive(Deserialize)]
struct FileCoverage {
    path: String,
    coverage: f64,
}

pub(super) fn handle_coverage(ctx: &CommandContext) -> Option<String> {
    let args = ctx.args.trim();

    match args {
        "run" => Some(run_coverage(ctx.effective_root())),
        "" => Some(read_cached_coverage(ctx.effective_root())),
        other => Some(format!(
            "Usage: `coverage` (cached) or `coverage run` (fresh)\n\nUnknown argument: `{other}`"
        )),
    }
}

/// Read coverage from `.coverage_report.json` (preferred) or `.coverage_baseline`
/// (legacy fallback) and return a formatted report.
fn read_cached_coverage(project_root: &std::path::Path) -> String {
    let report_path = project_root.join(COVERAGE_REPORT_FILE);

    if report_path.exists() {
        return read_coverage_report(&report_path);
    }

    // Legacy fallback: read the plain-text baseline file.
    read_legacy_baseline(project_root)
}

/// Parse and format `.coverage_report.json`.
fn read_coverage_report(path: &std::path::Path) -> String {
    let content = match std::fs::read_to_string(path) {
        Ok(c) => c,
        Err(e) => {
            return format!("**Coverage (cached)**\n\nError reading `.coverage_report.json`: {e}");
        }
    };

    let report: CoverageReport = match serde_json::from_str(&content) {
        Ok(r) => r,
        Err(e) => {
            return format!(
                "**Coverage (cached)**\n\nFailed to parse `.coverage_report.json`: {e}"
            );
        }
    };

    format_coverage_report(&report)
}

/// Format a `CoverageReport` as a human-readable message.
fn format_coverage_report(report: &CoverageReport) -> String {
    let mut out = "**Coverage (cached)**\n\n".to_string();
    out.push_str(&format!(
        "Overall: {:.1}% (threshold: {:.1}%)\n",
        report.overall, report.threshold
    ));

    // Top 5 lowest-covered files (already sorted ascending in the JSON, but sort
    // defensively here so the display is correct even if the file was hand-edited).
    let mut sorted: Vec<&FileCoverage> = report.files.iter().collect();
    sorted.sort_by(|a, b| {
        a.coverage
            .partial_cmp(&b.coverage)
            .unwrap_or(std::cmp::Ordering::Equal)
    });

    let targets: Vec<&FileCoverage> = sorted.into_iter().take(5).collect();
    if !targets.is_empty() {
        out.push_str("\n**Top 5 files needing coverage:**\n");
        for (i, file) in targets.iter().enumerate() {
            out.push_str(&format!(
                "{}. {} — {:.1}%\n",
                i + 1,
                file.path,
                file.coverage
            ));
        }
    }

    out.push_str("\n*Run `coverage run` for fresh results.*");
    out
}

/// Legacy fallback: read `.coverage_baseline` (plain text, one float per line).
fn read_legacy_baseline(project_root: &std::path::Path) -> String {
    let baseline_path = project_root.join(BASELINE_FILE);

    match std::fs::read_to_string(&baseline_path) {
        Ok(content) => {
            let lines: Vec<&str> = content.lines().filter(|l| !l.trim().is_empty()).collect();
            if lines.is_empty() {
                return "**Coverage (cached)**\n\nNo baseline data found.\n\n*Run `coverage run` to collect coverage.*".to_string();
            }
            format_legacy_baseline(&lines)
        }
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
            "**Coverage (cached)**\n\nNo `.coverage_report.json` or `.coverage_baseline` found.\n\n*Run `coverage run` to collect coverage.*".to_string()
        }
        Err(e) => format!("**Coverage (cached)**\n\nError reading `.coverage_baseline`: {e}"),
    }
}

/// Format legacy baseline lines for display.
fn format_legacy_baseline(lines: &[&str]) -> String {
    let mut out = "**Coverage (cached)**\n\n".to_string();
    if lines.len() == 2 {
        out.push_str(&format!("Rust:     {}%\n", lines[0].trim()));
        out.push_str(&format!("Frontend: {}%\n", lines[1].trim()));
    } else {
        out.push_str(&format!("Overall: {}%\n", lines[0].trim()));
    }
    out.push_str("\n*Run `coverage run` for fresh results.*");
    out
}

/// Run `script/test_coverage`, parse its summary, and return a formatted report.
fn run_coverage(project_root: &std::path::Path) -> String {
    let script_path = project_root.join(COVERAGE_SCRIPT);

    if !script_path.exists() {
        return format!(
            "**Coverage**\n\nCoverage script not found: `{COVERAGE_SCRIPT}`\n\nEnsure `{COVERAGE_SCRIPT}` exists in the project root."
        );
    }

    let output = std::process::Command::new("bash")
        .arg(&script_path)
        .current_dir(project_root)
        .output();

    match output {
        Err(e) => format!("**Coverage**\n\nFailed to run coverage script: {e}"),
        Ok(out) => {
            let stdout = String::from_utf8_lossy(&out.stdout);
            let stderr = String::from_utf8_lossy(&out.stderr);
            let combined = format!("{stdout}{stderr}");

            // Check if llvm-cov is missing
            if combined.contains("cargo-llvm-cov not available") && !combined.contains("TOTAL") {
                return "**Coverage**\n\n`cargo-llvm-cov` is not installed.\n\nInstall it with:\n```\ncargo install cargo-llvm-cov\n```".to_string();
            }

            // After the script runs, the .coverage_report.json is updated.
            // Read it to show per-file targets in the response.
            let report_path = project_root.join(COVERAGE_REPORT_FILE);
            if report_path.exists() {
                let mut result = read_coverage_report(&report_path);
                // Replace the "cached" label with "fresh".
                result = result.replacen("Coverage (cached)", "Coverage (fresh)", 1);
                // Replace the cached hint with a pass/fail indicator.
                let pass_indicator = if out.status.success() {
                    "PASS"
                } else {
                    "FAIL: coverage below threshold"
                };
                result =
                    result.replacen("*Run `coverage run` for fresh results.*", pass_indicator, 1);
                return result;
            }

            // Fallback: parse the text output as before.
            parse_coverage_output(&combined, out.status.success())
        }
    }
}

/// Parse the summary block from `script/test_coverage` output and format it.
/// Used as a fallback when `.coverage_report.json` is not available after a run.
fn parse_coverage_output(output: &str, passed: bool) -> String {
    let rust_cov = extract_line_value(output, "Rust line coverage:");
    let frontend_cov = extract_line_value(output, "Frontend line coverage:");
    let overall = extract_summary_field(output, "Overall:");
    let threshold = extract_summary_field(output, "Threshold:");

    let mut out = "**Coverage (fresh)**\n\n".to_string();

    if let Some(r) = &rust_cov {
        out.push_str(&format!("Rust:     {r}\n"));
    }
    if let Some(f) = &frontend_cov {
        out.push_str(&format!("Frontend: {f}\n"));
    }
    if let Some(o) = &overall {
        out.push_str(&format!("Overall:  {o}\n"));
    }
    if let Some(t) = &threshold {
        out.push_str(&format!("Threshold: {t}\n"));
    }

    if rust_cov.is_none() && frontend_cov.is_none() && overall.is_none() {
        // Could not parse — show raw output excerpt
        let excerpt: String = output
            .lines()
            .rev()
            .take(10)
            .collect::<Vec<_>>()
            .into_iter()
            .rev()
            .collect::<Vec<_>>()
            .join("\n");
        return format!("**Coverage (fresh)**\n\n```\n{excerpt}\n```");
    }

    if passed {
        out.push_str("\nPASS");
    } else {
        out.push_str("\nFAIL: coverage below threshold");
    }

    out
}

/// Extract a value from lines like `"Rust line coverage: 62.5%"`.
#[allow(clippy::string_slice)] // starts_with(prefix) guarantees prefix.len() is a char boundary
fn extract_line_value(output: &str, prefix: &str) -> Option<String> {
    output
        .lines()
        .find(|l| l.trim().starts_with(prefix))
        .map(|l| l.trim()[prefix.len()..].trim().to_string())
}

/// Extract a value from the summary block: `"  Overall:  62.5%"`.
#[allow(clippy::string_slice)] // starts_with(label) guarantees label.len() is a char boundary
fn extract_summary_field(output: &str, label: &str) -> Option<String> {
    output
        .lines()
        .find(|l| l.trim().starts_with(label))
        .map(|l| l.trim()[label.len()..].trim().to_string())
}

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

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

    fn make_ctx<'a>(
        services: &'a crate::services::Services,
        project_root: &'a std::path::Path,
        args: &'a str,
    ) -> super::super::CommandContext<'a> {
        super::super::CommandContext::new_test(services, args, "!test:example.com", project_root)
    }

    fn sample_coverage_report(overall: f64, threshold: f64, files: Vec<(&str, f64)>) -> String {
        let files_json: Vec<String> = files
            .iter()
            .map(|(path, cov)| format!(r#"{{"path":"{path}","coverage":{cov}}}"#))
            .collect();
        format!(
            r#"{{"overall":{overall},"threshold":{threshold},"files":[{}]}}"#,
            files_json.join(",")
        )
    }

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

    #[test]
    fn coverage_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("coverage"),
            "help should list coverage command: {output}"
        );
    }

    #[test]
    fn coverage_reads_coverage_report_json() {
        let dir = tempfile::tempdir().expect("tempdir");
        let report = sample_coverage_report(
            72.5,
            60.0,
            vec![
                ("server/src/low.rs", 15.0),
                ("server/src/medium.rs", 55.0),
                ("server/src/high.rs", 90.0),
            ],
        );
        std::fs::write(dir.path().join(".coverage_report.json"), &report).unwrap();

        let services =
            crate::services::Services::new_test(dir.path().to_path_buf(), "Timmy".to_string());
        let ctx = make_ctx(&services, dir.path(), "");
        let output = handle_coverage(&ctx).unwrap();

        assert!(output.contains("72.5"), "should include overall: {output}");
        assert!(
            output.contains("60.0"),
            "should include threshold: {output}"
        );
        assert!(
            output.contains("15.0"),
            "should include lowest-covered file pct: {output}"
        );
        assert!(
            output.contains("server/src/low.rs"),
            "should include lowest-covered file path: {output}"
        );
    }

    #[test]
    fn coverage_shows_top_5_lowest_files() {
        let dir = tempfile::tempdir().expect("tempdir");
        let files: Vec<(&str, f64)> = vec![
            ("a.rs", 10.0),
            ("b.rs", 20.0),
            ("c.rs", 30.0),
            ("d.rs", 40.0),
            ("e.rs", 50.0),
            ("f.rs", 60.0),
            ("g.rs", 70.0),
        ];
        let report = sample_coverage_report(40.0, 30.0, files);
        std::fs::write(dir.path().join(".coverage_report.json"), &report).unwrap();

        let services =
            crate::services::Services::new_test(dir.path().to_path_buf(), "Timmy".to_string());
        let ctx = make_ctx(&services, dir.path(), "");
        let output = handle_coverage(&ctx).unwrap();

        assert!(output.contains("a.rs"), "should show lowest file: {output}");
        assert!(
            output.contains("e.rs"),
            "should show 5th lowest file: {output}"
        );
        assert!(
            !output.contains("f.rs"),
            "should not show 6th file: {output}"
        );
        assert!(
            !output.contains("g.rs"),
            "should not show 7th file: {output}"
        );
    }

    #[test]
    fn coverage_falls_back_to_baseline_when_no_report_json() {
        let dir = tempfile::tempdir().expect("tempdir");
        std::fs::write(dir.path().join(".coverage_baseline"), "72.5\n").unwrap();

        let services =
            crate::services::Services::new_test(dir.path().to_path_buf(), "Timmy".to_string());
        let ctx = make_ctx(&services, dir.path(), "");
        let output = handle_coverage(&ctx).unwrap();

        assert!(
            output.contains("72.5"),
            "should report cached value from baseline: {output}"
        );
        assert!(
            output.contains("cached"),
            "response should indicate cached result: {output}"
        );
    }

    #[test]
    fn coverage_no_args_two_line_baseline() {
        let dir = tempfile::tempdir().expect("tempdir");
        std::fs::write(dir.path().join(".coverage_baseline"), "60.00\n65.21\n").unwrap();

        let services =
            crate::services::Services::new_test(dir.path().to_path_buf(), "Timmy".to_string());
        let ctx = make_ctx(&services, dir.path(), "");
        let output = handle_coverage(&ctx).unwrap();

        assert!(
            output.contains("60.00"),
            "should report first baseline value: {output}"
        );
        assert!(
            output.contains("65.21"),
            "should report second baseline value: {output}"
        );
    }

    #[test]
    fn coverage_missing_both_files_reports_clearly() {
        let dir = tempfile::tempdir().expect("tempdir");

        let services =
            crate::services::Services::new_test(dir.path().to_path_buf(), "Timmy".to_string());
        let ctx = make_ctx(&services, dir.path(), "");
        let output = handle_coverage(&ctx).unwrap();

        assert!(
            output.contains("coverage_report.json") || output.contains("coverage_baseline"),
            "missing files should mention a file name: {output}"
        );
    }

    #[test]
    fn coverage_run_missing_script_reports_error() {
        let dir = tempfile::tempdir().expect("tempdir");

        let services =
            crate::services::Services::new_test(dir.path().to_path_buf(), "Timmy".to_string());
        let ctx = make_ctx(&services, dir.path(), "run");
        let output = handle_coverage(&ctx).unwrap();

        assert!(
            output.contains("not found") || output.contains("script"),
            "missing script should produce a clear error: {output}"
        );
    }

    #[test]
    fn coverage_unknown_arg_returns_usage() {
        let dir = tempfile::tempdir().expect("tempdir");
        let services =
            crate::services::Services::new_test(dir.path().to_path_buf(), "Timmy".to_string());
        let ctx = make_ctx(&services, dir.path(), "blah");
        let output = handle_coverage(&ctx).unwrap();
        assert!(
            output.contains("Usage"),
            "unknown arg should show usage: {output}"
        );
    }

    #[test]
    fn coverage_command_works_via_dispatch() {
        let dir = tempfile::tempdir().expect("tempdir");
        let report = sample_coverage_report(55.0, 50.0, vec![]);
        std::fs::write(dir.path().join(".coverage_report.json"), &report).unwrap();

        let services =
            crate::services::Services::new_test(dir.path().to_path_buf(), "Timmy".to_string());
        let room_id = "!test:example.com".to_string();
        let dispatch = super::super::CommandDispatch {
            services: &services,
            project_root: &services.project_root,
            bot_user_id: "@timmy:homeserver.local",
            room_id: &room_id,
        };
        let result = super::super::try_handle_command(&dispatch, "@timmy coverage");
        assert!(
            result.is_some(),
            "coverage command must respond via dispatch (not fall through to LLM)"
        );
    }

    #[test]
    fn format_coverage_report_shows_overall_and_threshold() {
        let report = CoverageReport {
            overall: 66.25,
            threshold: 60.0,
            files: vec![
                FileCoverage {
                    path: "a.rs".to_string(),
                    coverage: 10.0,
                },
                FileCoverage {
                    path: "b.rs".to_string(),
                    coverage: 80.0,
                },
            ],
        };
        let result = format_coverage_report(&report);
        assert!(result.contains("66.2"), "should show overall: {result}");
        assert!(result.contains("60.0"), "should show threshold: {result}");
        assert!(result.contains("a.rs"), "should show lowest file: {result}");
        assert!(
            result.contains("10.0"),
            "should show lowest file pct: {result}"
        );
    }

    #[test]
    fn parse_coverage_output_extracts_fields() {
        let sample = "\
Rust line coverage: 62.5%\n\
Frontend line coverage: 70.0%\n\
=== Coverage Summary ===\n\
  Rust:     62.5%\n\
  Frontend: 70.0%\n\
  Overall:  66.25%\n\
  Threshold: 60.00%\n\
PASS: Coverage 66.25% meets threshold 60.00%\n\
";
        let result = parse_coverage_output(sample, true);
        assert!(
            result.contains("62.5"),
            "should include Rust coverage: {result}"
        );
        assert!(
            result.contains("70.0"),
            "should include Frontend coverage: {result}"
        );
        assert!(
            result.contains("66.25"),
            "should include Overall coverage: {result}"
        );
        assert!(result.contains("PASS"), "should indicate PASS: {result}");
    }

    #[test]
    fn extract_line_value_finds_rust_coverage() {
        let output = "Rust line coverage: 55.3%\n";
        let val = extract_line_value(output, "Rust line coverage:");
        assert_eq!(val.as_deref(), Some("55.3%"));
    }

    #[test]
    fn extract_summary_field_finds_overall() {
        let output = "  Overall:  72.1%\n";
        let val = extract_summary_field(output, "Overall:");
        assert_eq!(val.as_deref(), Some("72.1%"));
    }
}
