#!/bin/bash

# Update all report files with correct data from test results

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
cd "$SCRIPT_DIR"

# Function to extract data from test output and create report
create_report() {
    local decimals=$1
    local input_file="reports/run_${decimals}_output.txt"
    local output_file="reports/${decimals}_decimals.md"
    
    if [ ! -f "$input_file" ]; then
        echo "Warning: $input_file not found"
        return
    fi
    
    echo "Creating report for $decimals decimals..."
    
    # Create report header
    cat > "$output_file" << EOF
# Performance Report: $decimals Decimal$( [ "$decimals" -gt 1 ] && echo "s" )

## Test Environment

**Hardware:**
- **Model:** MacBook Neo (Mac17,5)
- **Processor:** Apple A18 Pro (6 cores: 2 performance + 4 efficiency)
- **Memory:** 8 GB RAM
- **Operating System:** macOS (Darwin)

**Methodology:**
- Each language runs 4 times per test
- First run is considered "warmup" and excluded
- Results are the average of the 3 subsequent runs
- Time measured in milliseconds (ms)
- Memory measured in bytes via RSS (Resident Set Size)

## Performance Summary

### All Languages

| Rank | Language | Time (ms) | Memory (bytes) | Instructions | Cycles | IPC | Type |
|------|-----------|-----------|----------------|--------------|---------|-----|------|
EOF
    
    # Extract data and sort by time, then memory
    grep "SUCCESS" "$input_file" | sed 's/\x1b\[[0-9;]*m//g' | \
    awk '{
        lang = $1
        # Time is the 3rd field (after "SUCCESS")
        time = $3
        # Memory is the 5th field (after "ms,")
        mem = $5
        # Remove commas from memory
        gsub(/,/, "", mem)
        print lang, time, mem
    }' | sort -k2 -n -k3 -n | \
    awk '{
        rank = NR
        lang = $1
        time = $2
        mem = $3
        
        # Determine type based on language
        if (lang == "Assembly" || lang == "C" || lang == "C++" || lang == "Rust" || \
            lang == "Go" || lang == "Nim" || lang == "Odin" || lang == "Fortran" || \
            lang == "Swift" || lang == "Crystal" || lang == "D" || lang == "Zig" || \
            lang == "Objective-C" || lang == "Haskell") {
            type = "Compiled"
        } else if (lang == "Java" || lang == "CSharp" || lang == "Kotlin" || \
                   lang == "Scala" || lang == "Dart" || lang == "Julia") {
            type = "JIT"
        } else {
            type = "Interpreted"
        }
        
        # Get instructions, cycles, and IPC from summary.csv
        summary_file = "data/" lang "/summary.csv"
        instructions = 0
        cycles = 0
        ipc = 0
        
        while ((getline line < summary_file) > 0) {
            if (line ~ /^instructions,/) {
                split(line, arr, ",")
                instructions = arr[2]
            }
            if (line ~ /^cycles,/) {
                split(line, arr, ",")
                cycles = arr[2]
            }
            if (line ~ /^ipc,/) {
                split(line, arr, ",")
                ipc = arr[2]
            }
        }
        close(summary_file)
        
        printf "| %d | %s | %s | %s | %s | %s | %.2f | %s |\n", rank, lang, time, mem, instructions, cycles, ipc, type
    }' >> "$output_file"
    
    # Add detailed results section
    cat >> "$output_file" << EOF

## Detailed Results

See the full test output in \`reports/run_${decimals}_output.txt\`.

---
*Generated from Pi Calculation Benchmark - Apple A18 Pro Performance Study*
EOF
    
    echo "✓ Created $output_file"
}

# Create reports for all decimal levels
for decimals in 1 2 5 10 100 1000 2000; do
    create_report "$decimals"
done

echo ""
echo "=== All reports updated ==="