#!/usr/bin/env python3
"""Update reports with actual profiling data."""

import os
from pathlib import Path

# Profiling data from actual measurements (100 decimals)
PROFILING_DATA = {
    "Assembly": {"startup": 5, "calculation": 0, "io": 1, "total": 5},
    "C": {"startup": 4, "calculation": 0, "io": 2, "total": 5},
    "C++": {"startup": 2, "calculation": 1, "io": 2, "total": 5},
    "Rust": {"startup": 4, "calculation": 0, "io": 1, "total": 5},
    "Go": {"startup": 2, "calculation": 6, "io": 1, "total": 9},
    "Nim": {"startup": 1, "calculation": 2, "io": 2, "total": 5},
    "Odin": {"startup": 4, "calculation": 0, "io": 2, "total": 5},
    "Fortran": {"startup": 1, "calculation": 5, "io": 1, "total": 7},
    "Swift": {"startup": 4, "calculation": 3, "io": 1, "total": 8},
    "Crystal": {"startup": 3, "calculation": 2, "io": 2, "total": 7},
    "Java": {"startup": 20, "calculation": 55, "io": 2, "total": 77},
    "C#": {"startup": 24, "calculation": 52, "io": 2, "total": 78},
    "Kotlin": {"startup": 43, "calculation": 4, "io": 1, "total": 48},
    "Julia": {"startup": 36, "calculation": 331, "io": 1, "total": 368},
    "Python": {"startup": 11, "calculation": 32, "io": 2, "total": 45},
    "Perl": {"startup": 24, "calculation": 17, "io": 1, "total": 42},
    "PHP": {"startup": 12, "calculation": 78, "io": 1, "total": 91},
    "Ruby": {"startup": 25, "calculation": 46, "io": 1, "total": 72},
    "JavaScript": {"startup": 17, "calculation": 82, "io": 1, "total": 100},
}

def generate_gantt_chart(lang, data):
    """Generate Gantt chart for a language."""
    startup = data['startup']
    calculation = data['calculation']
    io = data['io']
    total = data['total']
    
    # Calculate percentages
    startup_pct = (startup / total) * 100 if total > 0 else 0
    calc_pct = (calculation / total) * 100 if total > 0 else 0
    io_pct = (io / total) * 100 if total > 0 else 0
    
    # Generate Mermaid Gantt chart
    gantt = f"""
```mermaid
gantt
    title {lang} - Execution Time Breakdown
    dateFormat X
    axisFormat %ms
    
    section Startup
    Runtime Init    :0, {startup}
    
    section Calculation
    π Calculation    :{startup}, {startup + calculation}
    
    section I/O
    Output          :{startup + calculation}, {total}
```

**Time Breakdown:**
- **Startup**: {startup} ms ({startup_pct:.1f}%)
- **Calculation**: {calculation} ms ({calc_pct:.1f}%)
- **I/O**: {io} ms ({io_pct:.1f}%)
- **Total**: {total} ms
"""
    
    return gantt

def update_reports_with_profiling():
    """Update all reports with actual profiling data."""
    
    reports = [
        ("reports/100_decimals.md", 100),
    ]
    
    for filename, decimals in reports:
        if not os.path.exists(filename):
            print(f"File not found: {filename}")
            continue
        
        with open(filename, 'r') as f:
            content = f.read()
        
        # Remove old Gantt section if exists
        if "## Execution Time Breakdown (Gantt Charts)" in content:
            parts = content.split("## Execution Time Breakdown (Gantt Charts)", 1)
            if "## Key Findings" in parts[1]:
                parts2 = parts[1].split("## Key Findings", 1)
                content = parts[0] + "## Key Findings" + parts2[1]
        
        # Add new Gantt section before "Key Findings"
        gantt_section = "\n## Execution Time Breakdown (Gantt Charts)\n\n"
        gantt_section += "The following Gantt charts show the execution time breakdown for each language:\n\n"
        
        # Add Gantt charts for each language
        for lang, data in PROFILING_DATA.items():
            gantt_chart = generate_gantt_chart(lang, data)
            gantt_section += gantt_chart
        
        # Insert before "Key Findings"
        if "## Key Findings" in content:
            parts = content.split("## Key Findings", 1)
            updated_content = parts[0] + gantt_section + "## Key Findings" + parts[1]
        else:
            updated_content = content + gantt_section
        
        with open(filename, 'w') as f:
            f.write(updated_content)
        
        print(f"Updated {filename} with actual profiling data")

if __name__ == "__main__":
    update_reports_with_profiling()
    print("\nReports updated with actual profiling data!")