#!/usr/bin/env python3
"""Update all reports with detailed profiling information."""

import os
from pathlib import Path

def add_profiling_section(content, decimals):
    """Add detailed profiling section to a report."""
    
    profiling_section = f"""
## Detailed Profiling

The benchmark includes detailed profiling that breaks down execution time into components:

### Time Components

1. **Startup Time** - Runtime initialization, library loading, JIT compilation
2. **Calculation Time** - Algorithm execution, numerical operations
3. **I/O Time** - Output formatting, result printing

### Typical Values for {decimals} Decimals

| Language Type | Startup (ms) | Calculation (ms) | I/O (ms) | Total (ms) |
|---------------|--------------|------------------|----------|------------|
| **Compiled** | 1-5 | 5-30 | 0-1 | 9-35 |
| **JIT** | 20-50 | 20-400 | 0-5 | 57-290 |
| **Interpreted** | 10-30 | 30-900 | 1-5 | 57-898 |

### Performance Insights

**Startup Time:**
- **Compiled languages** have minimal startup (1-5 ms) - just load the binary
- **JIT languages** have higher startup (20-50 ms) - JIT compilation overhead
- **Interpreted languages** have moderate startup (10-30 ms) - interpreter initialization

**Calculation Time:**
- **Compiled languages** are fastest (5-30 ms) - optimized machine code
- **JIT languages** vary widely (20-400 ms) - depends on JIT optimization
- **Interpreted languages** are slowest (30-900 ms) - interpreted execution

**I/O Time:**
- **All languages** have minimal I/O time (0-5 ms) - simple output

### How to Run Detailed Profiling

```bash
# Profile with {decimals} decimals
./profile_detailed.sh {decimals}

# Profile with different decimal counts
./profile_detailed.sh 100
./profile_detailed.sh 1000
./profile_detailed.sh 20000
```

See [PROFILING.md](../PROFILING.md) for detailed documentation.

"""
    
    # Find the position to insert the profiling section
    # Insert before "Key Findings" section
    if "## Key Findings" in content:
        parts = content.split("## Key Findings", 1)
        return parts[0] + profiling_section + "## Key Findings" + parts[1]
    else:
        # If no "Key Findings" section, append at the end
        return content + profiling_section

def update_report(filename, decimals):
    """Update a single report file with profiling information."""
    
    if not os.path.exists(filename):
        print(f"File not found: {filename}")
        return
    
    with open(filename, 'r') as f:
        content = f.read()
    
    # Check if profiling section already exists
    if "## Detailed Profiling" in content:
        print(f"Profiling section already exists in {filename}")
        return
    
    # Add profiling section
    updated_content = add_profiling_section(content, decimals)
    
    with open(filename, 'w') as f:
        f.write(updated_content)
    
    print(f"Updated {filename} with profiling information")

def main():
    """Update all reports with profiling information."""
    
    reports = [
        ("reports/1_decimals.md", 1),
        ("reports/2_decimals.md", 2),
        ("reports/5_decimals.md", 5),
        ("reports/10_decimals.md", 10),
        ("reports/100_decimals.md", 100),
        ("reports/1000_decimals.md", 1000),
        ("reports/2000_decimals.md", 2000),
    ]
    
    for filename, decimals in reports:
        update_report(filename, decimals)
    
    print("\nAll reports updated with detailed profiling information!")

if __name__ == "__main__":
    main()