#!/usr/bin/env bash
# Bulk Aion repair: reprocess 98K day-boundary gaps via repair_direct_parquet.py
# Designed to run on bigblack via: pueue add -- bash scripts/bulk_aion_repair.sh
#
# Stages:
#   1. Pre-flight dry run (gap counts per symbol, no writes)
#   2. Execute repair (--all --execute --workers 24)
#   3. Post-repair Stathera audit (PASS if <100 gaps remain)
set -euo pipefail

# --- Configuration ---
WORKERS=24
PYTHON_VERSION=3.13
REPO_DIR="$(cd "$(dirname "$0")/.." && pwd)"
TIMESTAMP="$(date -u +%Y%m%d_%H%M%S)"
LOGFILE="/tmp/bulk_aion_repair_${TIMESTAMP}.log"
WALL_START="$(date +%s)"

# --- Helpers ---
log() {
    echo "[$(date -u +%Y-%m-%dT%H:%M:%SZ)] $*"
}

cleanup() {
    local exit_code=$?
    local elapsed=$(( $(date +%s) - WALL_START ))
    log "Script exiting with code ${exit_code} after ${elapsed}s"
    if [[ ${exit_code} -ne 0 ]]; then
        log "FAILED -- see full log: ${LOGFILE}"
    fi
}
trap cleanup EXIT

# --- Main (all output tee'd to log) ---
{
    cd "${REPO_DIR}"
    log "bulk_aion_repair.sh starting"
    log "Repo: ${REPO_DIR}"
    log "Workers: ${WORKERS}"
    log "Log: ${LOGFILE}"
    log ""

    # ---------------------------------------------------------------
    # Stage 1: Pre-flight dry run (no --execute)
    # ---------------------------------------------------------------
    log "=== STAGE 1: Pre-flight dry run ==="
    uv run --python "${PYTHON_VERSION}" python scripts/repair_direct_parquet.py --all
    log "=== STAGE 1 COMPLETE ==="
    log ""

    # ---------------------------------------------------------------
    # Stage 2: Execute repair
    # ---------------------------------------------------------------
    log "=== STAGE 2: Execute repair (--all --execute --workers ${WORKERS}) ==="
    uv run --python "${PYTHON_VERSION}" python scripts/repair_direct_parquet.py \
        --all --execute --workers "${WORKERS}"
    log "=== STAGE 2 COMPLETE ==="
    log ""

    # ---------------------------------------------------------------
    # Stage 3: Post-repair Stathera audit
    # ---------------------------------------------------------------
    log "=== STAGE 3: Post-repair Stathera audit ==="
    AUDIT_JSON="$(uv run --python "${PYTHON_VERSION}" python scripts/detect_gaps.py --json 2>/dev/null || true)"

    if [[ -z "${AUDIT_JSON}" ]]; then
        log "WARNING: Stathera audit produced no JSON output"
        log "RESULT: INCONCLUSIVE (audit failed)"
    else
        # Extract total gap count from JSON. The detect_gaps.py --json output
        # has a top-level "total_gaps" field.
        TOTAL_GAPS="$(echo "${AUDIT_JSON}" | python3 -c "import sys,json; d=json.load(sys.stdin); print(d.get('total_gaps', d.get('gaps', -1)))" 2>/dev/null || echo "-1")"

        log "Stathera total gaps: ${TOTAL_GAPS}"

        if [[ "${TOTAL_GAPS}" -eq -1 ]]; then
            log "WARNING: Could not parse gap count from audit JSON"
            log "Raw JSON (first 500 chars): ${AUDIT_JSON:0:500}"
            log "RESULT: INCONCLUSIVE"
        elif [[ "${TOTAL_GAPS}" -lt 100 ]]; then
            log "RESULT: PASS (${TOTAL_GAPS} gaps < 100 threshold)"
        else
            log "RESULT: FAIL (${TOTAL_GAPS} gaps >= 100 threshold)"
        fi
    fi
    log "=== STAGE 3 COMPLETE ==="
    log ""

    # ---------------------------------------------------------------
    # Summary
    # ---------------------------------------------------------------
    WALL_END="$(date +%s)"
    WALL_ELAPSED=$(( WALL_END - WALL_START ))
    HOURS=$(( WALL_ELAPSED / 3600 ))
    MINS=$(( (WALL_ELAPSED % 3600) / 60 ))
    SECS=$(( WALL_ELAPSED % 60 ))
    log "=== BULK AION REPAIR COMPLETE ==="
    log "Wall clock: ${HOURS}h ${MINS}m ${SECS}s"
    log "Full log: ${LOGFILE}"

} 2>&1 | tee "${LOGFILE}"
