#!/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'

def get_color(lang):
    lang_type = get_type(lang)
    if lang_type == 'Compiled':
        return '#2ecc71'  # Green
    elif lang_type == 'JIT':
        return '#3498db'  # Blue
    else:
        return '#e74c3c'  # Red

# Generate Mermaid diagrams for each decimal level
for decimals in [1, 2, 5, 10, 100, 1000, 2000]:
    print(f"Creating Mermaid diagrams for {decimals} decimals...")
    
    # Sort by time
    sorted_by_time = sorted(data.keys(), key=lambda x: data[x].get('time_ms', float('inf')))
    
    # Create Mermaid diagram for time comparison (top 20)
    mermaid_time = f"""```mermaid
xychart-beta
    title "Execution Time Comparison - {decimals} Decimal{'s' if decimals > 1 else ''}"
    x-axis [{', '.join([f'"{lang}"' for lang in sorted_by_time[:20]])}]
    y-axis "Time (ms)" 0 --> {int(data[sorted_by_time[0]]['time_ms'] * 1.2)}
    bar [{', '.join([str(int(data[lang]['time_ms'])) for lang in sorted_by_time[:20]])}]
```
"""
    
    # Create Mermaid diagram for memory comparison (top 20)
    sorted_by_memory = sorted(data.keys(), key=lambda x: data[x].get('memory_bytes', float('inf')))
    mermaid_memory = f"""```mermaid
xychart-beta
    title "Memory Usage Comparison - {decimals} Decimal{'s' if decimals > 1 else ''}"
    x-axis [{', '.join([f'"{lang}"' for lang in sorted_by_memory[:20]])}]
    y-axis "Memory (MB)" 0 --> {int(data[sorted_by_memory[0]]['memory_bytes'] / (1024 * 1024) * 1.2)}
    bar [{', '.join([str(int(data[lang]['memory_bytes'] / (1024 * 1024))) for lang in sorted_by_memory[:20]])}]
```
"""
    
    # Create Mermaid diagram for IPC comparison (top 20)
    sorted_by_ipc = sorted(data.keys(), key=lambda x: data[x].get('ipc', 0), reverse=True)
    mermaid_ipc = f"""```mermaid
xychart-beta
    title "CPU Efficiency (IPC) Comparison - {decimals} Decimal{'s' if decimals > 1 else ''}"
    x-axis [{', '.join([f'"{lang}"' for lang in sorted_by_ipc[:20]])}]
    y-axis "IPC (Instructions Per Cycle)" 0 --> {int(data[sorted_by_ipc[0]]['ipc'] * 1.2)}
    bar [{', '.join([str(round(data[lang]['ipc'], 2)) for lang in sorted_by_ipc[:20]])}]
```
"""
    
    # Create Mermaid diagram for time vs memory (scatter plot)
    # Using a simple bar chart for now since Mermaid doesn't have scatter plots
    mermaid_scatter = f"""```mermaid
graph TD
    subgraph "Time vs Memory Trade-off - {decimals} Decimal{'s' if decimals > 1 else ''}"
{chr(10).join([f'    {lang}["{lang}<br/>Time: {int(data[lang]["time_ms"])}ms<br/>Memory: {int(data[lang]["memory_bytes"] / (1024 * 1024))}MB"]' for lang in sorted_by_time[:10]])}
    end
```
"""
    
    # Save Mermaid diagrams to file
    output_file = f'reports/{decimals}_decimals_mermaid.md'
    with open(output_file, 'w') as f:
        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_by_time, 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")
        
        # Add Mermaid diagrams
        f.write("\n## Visualizations\n\n")
        f.write("### Execution Time Comparison\n\n")
        f.write(mermaid_time)
        f.write("\n### Memory Usage Comparison\n\n")
        f.write(mermaid_memory)
        f.write("\n### CPU Efficiency (IPC) Comparison\n\n")
        f.write(mermaid_ipc)
        f.write("\n### Time vs Memory Trade-off\n\n")
        f.write(mermaid_scatter)
        
        # Footer
        f.write(f"\n## 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 Mermaid diagrams created ===")