#!/bin/bash
set -uo pipefail
# No set -e: forge may fail on individual runs, we want to continue

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

LOG="analysis/FUZZING_LOG.md"
MAX_DISK_PCT=85

# Initialize log if not exists
if [ ! -f "$LOG" ]; then
    echo "# Deep Fuzzing Log" > "$LOG"
    echo "" >> "$LOG"
    echo "Started: $(date -u '+%Y-%m-%d %H:%M UTC')" >> "$LOG"
    echo "" >> "$LOG"
fi

check_resources() {
    local disk_pct=$(df / --output=pcent | tail -1 | tr -d ' %')
    if [ "$disk_pct" -gt "$MAX_DISK_PCT" ]; then
        echo "DISK FULL ($disk_pct%) — stopping" | tee -a "$LOG"
        /tmp/supervisor-notify.sh "DISK FULL ($disk_pct%) — fuzzing stopped" 2>/dev/null || true
        exit 1
    fi
    echo "Disk: ${disk_pct}%"
}

# Run ONE fuzzing run per forge invocation (avoids EVM MemoryOOG)
run_single() {
    local ci=$1 as=$2 aw=$3 dd=$4 buybias=$5 trades=$6
    local seed=$RANDOM$RANDOM$RANDOM
    
    CI_VALUE=$ci \
    AS_VALUE=$as \
    AW_VALUE=$aw \
    DD_VALUE=$dd \
    BATCH_SEED=$seed \
    OPTIMIZER_CLASS=ConfigurableOptimizer \
    UNCAPPED_SWAPS=true \
    FUZZING_RUNS=1 \
    BUY_BIAS=$buybias \
    TRADES_PER_RUN=$trades \
    forge script analysis/StreamlinedFuzzing.s.sol:StreamlinedFuzzing \
        --skip-simulation --gas-estimate-multiplier 300 -vv 2>&1 | tail -5
}

run_batch() {
    local batch_num=$1 ci=$2 as=$3 aw=$4 dd=$5 buybias=$6 trades=$7 runs=$8 note=$9
    
    echo "" >> "$LOG"
    echo "## Batch $batch_num: $note" >> "$LOG"
    echo "- Started: $(date -u '+%Y-%m-%d %H:%M UTC')" >> "$LOG"
    echo "- Params: CI=$ci AS=$as AW=$aw DD=$dd buybias=$buybias trades=$trades runs=$runs" >> "$LOG"
    
    echo "=== BATCH $batch_num: $note ($runs runs × $trades trades) ==="
    
    local profitable=0
    local total=0
    local findings=""
    
    for i in $(seq 1 $runs); do
        echo -n "  Run $i/$runs... "
        
        run_single $ci $as $aw $dd $buybias $trades
        
        # Find the most recently modified CSV (forge overwrites same name)
        local new_csv=$(ls -1t analysis/fuzz-*.csv 2>/dev/null | head -1)
        
        if [ -n "$new_csv" ] && [ -f "$new_csv" ]; then
            # Rename to unique name to preserve it
            local unique_name="analysis/fuzz-b${batch_num}-$(printf '%03d' $i).csv"
            cp "$new_csv" "$unique_name"
            
            local init_eth=$(grep "^INIT," "$unique_name" 2>/dev/null | cut -d',' -f13)
            local final_eth=$(grep "^FINAL," "$unique_name" 2>/dev/null | cut -d',' -f13)
            
            if [ -n "$init_eth" ] && [ -n "$final_eth" ]; then
                total=$((total + 1))
                local pnl=$(python3 -c "print(f'{($final_eth - $init_eth)/1e18:+.1f}')" 2>/dev/null || echo "?")
                
                if python3 -c "exit(0 if $final_eth > $init_eth else 1)" 2>/dev/null; then
                    profitable=$((profitable + 1))
                    findings="$findings\n  - $(basename $unique_name): PnL=${pnl} ETH"
                    echo "PROFITABLE (${pnl} ETH)"
                else
                    echo "safe (${pnl} ETH)"
                    # Delete non-profitable CSVs to save disk
                    rm -f "$unique_name"
                fi
            else
                echo "no data"
                rm -f "$unique_name"
            fi
        else
            echo "failed (no CSV)"
        fi
        
        # Check disk every 10 runs
        if [ $((i % 10)) -eq 0 ]; then
            check_resources
        fi
    done
    
    echo "- Completed: $(date -u '+%Y-%m-%d %H:%M UTC')" >> "$LOG"
    echo "- Results: $profitable/$total profitable" >> "$LOG"
    
    if [ $profitable -gt 0 ]; then
        echo -e "- Profitable runs:$findings" >> "$LOG"
        echo "*** BATCH $batch_num: $profitable/$total PROFITABLE ***"
    else
        echo "- No profitable runs found" >> "$LOG"
        echo "Batch $batch_num: 0/$total profitable"
    fi
    echo "" >> "$LOG"
}

echo "=== DEEP FUZZING SEARCH (1 run per forge invocation) ==="
echo "Running continuously. Logging to $LOG"
echo ""

# Batch 2: buy-heavy
check_resources
run_batch 2 0 100000000000000000 20 500000000000000000 70 2000 30 "buy-heavy CI=0"

check_resources
run_batch 3 0 100000000000000000 20 500000000000000000 30 2000 30 "sell-heavy CI=0"

check_resources
run_batch 4 0 500000000000000000 50 500000000000000000 50 2000 30 "wider AS=50% AW=50"

check_resources
run_batch 5 0 100000000000000000 20 500000000000000000 50 5000 15 "extra deep 5000 trades"

check_resources
run_batch 6 100000000000000000 100000000000000000 20 500000000000000000 50 2000 30 "CI=10%"

check_resources
run_batch 7 0 300000000000000000 40 500000000000000000 60 2000 30 "mid AS=30% AW=40"

check_resources
run_batch 8 0 1000000000000000000 80 500000000000000000 50 2000 30 "high AS=100% AW=80"

check_resources
run_batch 9 0 100000000000000000 20 100000000000000000 50 2000 30 "low DD=10%"

check_resources
run_batch 10 0 100000000000000000 20 1000000000000000000 50 2000 30 "high DD=100%"

check_resources
run_batch 11 0 100000000000000000 20 500000000000000000 50 3000 20 "3000 trades"

check_resources
run_batch 12 0 100000000000000000 20 500000000000000000 90 2000 30 "extreme buy 90%"

check_resources
run_batch 13 200000000000000000 100000000000000000 20 500000000000000000 50 2000 30 "CI=20%"

check_resources
run_batch 14 0 100000000000000000 100 500000000000000000 50 2000 30 "max AW=100"

check_resources
run_batch 15 0 100000000000000000 20 500000000000000000 50 2000 30 "baseline repeat"

check_resources
run_batch 16 0 200000000000000000 30 500000000000000000 55 2000 30 "AS=20% AW=30"

check_resources
run_batch 17 50000000000000000 100000000000000000 20 500000000000000000 50 2000 30 "CI=5%"

check_resources
run_batch 18 0 100000000000000000 20 500000000000000000 50 4000 15 "4000 trades"

check_resources
run_batch 19 0 100000000000000000 20 500000000000000000 65 3000 20 "3000 trades buybias=65"

check_resources
run_batch 20 0 100000000000000000 20 500000000000000000 50 2000 50 "baseline big batch"

echo "" >> "$LOG"
echo "## Search Complete" >> "$LOG"
echo "All batches finished: $(date -u '+%Y-%m-%d %H:%M UTC')" >> "$LOG"

echo "=== ALL BATCHES COMPLETE ==="
/tmp/supervisor-notify.sh "Deep fuzzing complete — all 20 batches finished" 2>/dev/null || true
