fn format_comprehensive_report(
    report: &ComprehensiveReport,
    format: ComprehensiveOutputFormat,
    executive_summary: bool,
) -> Result<String> {
    match format {
        ComprehensiveOutputFormat::Json => format_comp_as_json(report),
        ComprehensiveOutputFormat::Markdown => format_comp_as_markdown(report, executive_summary),
        _ => Ok("Comprehensive analysis completed.".to_string()),
    }
}

// Helper: Format comprehensive report as JSON
fn format_comp_as_json(report: &ComprehensiveReport) -> Result<String> {
    Ok(serde_json::to_string_pretty(report)?)
}

// Helper: Format comprehensive report as Markdown
fn format_comp_as_markdown(
    report: &ComprehensiveReport,
    executive_summary: bool,
) -> Result<String> {
    use std::fmt::Write;
    let mut output = String::new();

    writeln!(&mut output, "# Comprehensive Code Analysis Report\n")?;

    if executive_summary {
        write_comp_executive_summary(&mut output)?;
    }

    write_comp_analysis_sections(&mut output, report)?;

    Ok(output)
}

// Helper: Write executive summary
fn write_comp_executive_summary(output: &mut String) -> Result<()> {
    use std::fmt::Write;
    writeln!(output, "## Executive Summary\n")?;
    writeln!(
        output,
        "This report provides a comprehensive analysis of code quality metrics.\n"
    )?;
    Ok(())
}

// Helper: Write all analysis sections
fn write_comp_analysis_sections(output: &mut String, report: &ComprehensiveReport) -> Result<()> {
    if let Some(complexity) = &report.complexity {
        write_comp_complexity_section(output, complexity)?;
    }

    if let Some(satd) = &report.satd {
        write_comp_satd_section(output, satd)?;
    }

    if let Some(tdg) = &report.tdg {
        write_comp_tdg_section(output, tdg)?;
    }

    if let Some(dead_code) = &report.dead_code {
        write_comp_dead_code_section(output, dead_code)?;
    }

    if let Some(defects) = &report.defects {
        write_comp_defects_section(output, defects)?;
    }

    if let Some(duplicates) = &report.duplicates {
        write_comp_duplicates_section(output, duplicates)?;
    }

    Ok(())
}

// Helper: Write complexity section
fn write_comp_complexity_section(output: &mut String, complexity: &ComplexityReport) -> Result<()> {
    use std::fmt::Write;
    writeln!(output, "## Complexity Analysis\n")?;
    writeln!(output, "- Total functions: {}", complexity.total_functions)?;
    writeln!(
        output,
        "- High complexity functions: {}",
        complexity.high_complexity_count
    )?;
    writeln!(
        output,
        "- Average complexity: {:.2}",
        complexity.average_complexity
    )?;
    writeln!(output, "- P99 complexity: {}\n", complexity.p99_complexity)?;
    Ok(())
}

// Helper: Write SATD section
fn write_comp_satd_section(output: &mut String, satd: &SatdReport) -> Result<()> {
    use std::fmt::Write;
    writeln!(output, "## Technical Debt (SATD)\n")?;
    writeln!(output, "- Total items: {}", satd.total_items)?;
    writeln!(output, "- By type:")?;
    for (t, count) in &satd.by_type {
        writeln!(output, "  - {t}: {count}")?;
    }
    writeln!(output)?;
    Ok(())
}

// Helper: Write TDG section
fn write_comp_tdg_section(output: &mut String, tdg: &TdgReport) -> Result<()> {
    use std::fmt::Write;
    writeln!(output, "## Technical Debt Gradient\n")?;
    writeln!(output, "- Average TDG: {:.2}", tdg.average_tdg)?;
    writeln!(output, "- Critical files: {}", tdg.critical_files.len())?;
    writeln!(output, "- Hotspot count: {}\n", tdg.hotspot_count)?;
    Ok(())
}

// Helper: Write dead code section
fn write_comp_dead_code_section(output: &mut String, dead_code: &DeadCodeReport) -> Result<()> {
    use std::fmt::Write;
    writeln!(output, "## Dead Code\n")?;
    writeln!(output, "- Total items: {}", dead_code.total_items)?;
    writeln!(
        output,
        "- Percentage: {:.1}%\n",
        dead_code.dead_code_percentage
    )?;
    Ok(())
}

// Helper: Write defects section
fn write_comp_defects_section(output: &mut String, defects: &DefectReport) -> Result<()> {
    use std::fmt::Write;
    writeln!(output, "## Defect Prediction\n")?;
    writeln!(output, "- Total analyzed: {}", defects.total_analyzed)?;
    writeln!(output, "- High risk files: {}\n", defects.high_risk_count)?;
    Ok(())
}

// Helper: Write duplicates section
fn write_comp_duplicates_section(output: &mut String, duplicates: &DuplicateReport) -> Result<()> {
    use std::fmt::Write;
    writeln!(output, "## Code Duplication\n")?;
    writeln!(
        output,
        "- Duplicate blocks: {}",
        duplicates.duplicate_blocks
    )?;
    writeln!(output, "- Duplicate lines: {}", duplicates.duplicate_lines)?;
    writeln!(
        output,
        "- Percentage: {:.1}%\n",
        duplicates.duplicate_percentage
    )?;
    Ok(())
}

// Incremental coverage stub data structures
