#!/usr/bin/env python3
import os
import csv
import json

# Read analysis data
with open('data/analysis.json', 'r') as f:
    data = json.load(f)

# Define language types
compiled = ['Assembly', 'C', 'C++', 'Rust', 'Go', 'Nim', 'Odin', 'Fortran', 'Swift', 'Crystal', 'D', 'Zig', 'Objective-C', 'Haskell']
jit = ['Java', 'CSharp', 'Kotlin', 'Scala', 'Dart', 'Julia']
interpreted = ['Python', 'JavaScript', 'TypeScript', 'Ruby', 'PHP', 'Perl', 'Lua', 'Bash', 'Brainfuck', 'Elixir', 'Erlang', 'R']

def get_type(lang):
    if lang in compiled:
        return 'Compiled'
    elif lang in jit:
        return 'JIT'
    else:
        return 'Interpreted'

# Generate reports for each decimal level
for decimals in [1, 2, 5, 10, 100, 1000, 2000]:
    output_file = f'reports/{decimals}_decimals.md'
    
    # Sort by time, then memory
    sorted_langs = sorted(data.keys(), key=lambda x: (data[x].get('time_ms', float('inf')), data[x].get('memory_bytes', float('inf'))))
    
    # Create report
    with open(output_file, 'w') as f:
        # Header
        f.write(f"# Performance Report: {decimals} Decimal{'s' if decimals > 1 else ''}\n\n")
        f.write("## Test Environment\n\n")
        f.write("**Hardware:**\n")
        f.write("- **Model:** MacBook Neo (Mac17,5)\n")
        f.write("- **Processor:** Apple A18 Pro (6 cores: 2 performance + 4 efficiency)\n")
        f.write("- **Memory:** 8 GB RAM\n")
        f.write("- **Operating System:** macOS (Darwin)\n\n")
        f.write("**Methodology:**\n")
        f.write("- Each language runs 4 times per test\n")
        f.write("- First run is considered \"warmup\" and excluded\n")
        f.write("- Results are the average of the 3 subsequent runs\n")
        f.write("- Time measured in milliseconds (ms)\n")
        f.write("- Memory measured in bytes via RSS (Resident Set Size)\n\n")
        f.write("## Performance Summary\n\n")
        f.write("### All Languages\n\n")
        f.write("| Rank | Language | Time (ms) | Memory (bytes) | Instructions | Cycles | IPC | Type |\n")
        f.write("|------|-----------|-----------|----------------|--------------|---------|-----|------|\n")
        
        # Add data
        for rank, lang in enumerate(sorted_langs, 1):
            d = data[lang]
            time_ms = int(d.get('time_ms', 0))
            memory = int(d.get('memory_bytes', 0))
            instructions = int(d.get('instructions', 0))
            cycles = int(d.get('cycles', 0))
            ipc = d.get('ipc', 0)
            lang_type = get_type(lang)
            
            f.write(f"| {rank} | {lang} | {time_ms} | {memory} | {instructions} | {cycles} | {ipc:.2f} | {lang_type} |\n")
        
        # Footer
        f.write(f"\n## Visualizations\n\n")
        f.write(f"![Performance Comparison](100_decimals.png)\n\n")
        f.write(f"## Detailed Results\n\n")
        f.write(f"See the full test output in `reports/run_{decimals}_output.txt`.\n\n")
        f.write("---\n")
        f.write("*Generated from Pi Calculation Benchmark - Apple A18 Pro Performance Study*\n")
    
    print(f"✓ Created {output_file}")

print("\n=== All reports updated ===")