#!/bin/bash
# Run all 10 batches sequentially. Safe: if one fails, the rest don't run.
# Each batch saves corrections to JSON before applying to DB.
#
# Usage: bash /app/scripts/gpre0_run_all.sh
#        bash /app/scripts/gpre0_run_all.sh 5   # start from batch 5

set -e

START=${1:-1}
TOTAL=10

echo "=== Starting from batch $START of $TOTAL ==="

for i in $(seq $START $TOTAL); do
    echo ""
    echo "================================================================"
    echo "  BATCH $i/$TOTAL — $(date)"
    echo "================================================================"

    PYTHONPATH=/app python3 /app/scripts/gpre0_validate_hints.py \
        --batch-id $i \
        --total-batches $TOTAL \
        --batch-size 20

    EXIT_CODE=$?
    if [ $EXIT_CODE -ne 0 ]; then
        echo "BATCH $i FAILED with exit code $EXIT_CODE"
        echo "Resume with: bash /app/scripts/gpre0_run_all.sh $i"
        exit $EXIT_CODE
    fi

    echo "BATCH $i DONE — $(date)"
done

echo ""
echo "ALL $TOTAL BATCHES COMPLETE!"
