#!/bin/bash
set -e

# End-to-end Docker test for Harbor tasks
# Usage: ./docker_e2e_test.sh [task_name] [--debug|nodebug]

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
TASKS_DIR="$SCRIPT_DIR/codeflash-perf-tasks"
BASE_IMAGE="codeflash-inference-base:latest"

# Build base image if not exists
if ! docker image inspect "$BASE_IMAGE" &>/dev/null; then
    echo "Building base image..."
    docker build -t "$BASE_IMAGE" "$TASKS_DIR/base-image/"
fi

TASK_NAME="${1:-detection_event_log-detectioneventlogblockv1-_evict_oldest_video}"
DEBUG_FLAG=""
if [ "${2}" = "--debug" ]; then
    DEBUG_FLAG="--debug"
fi

TASK_DIR="$TASKS_DIR/$TASK_NAME"

if [ ! -d "$TASK_DIR" ]; then
    echo "Task not found: $TASK_DIR"
    find "$TASKS_DIR" -maxdepth 1 -type d | head -20
    exit 1
fi

echo "============================================================"
echo "Testing task: $TASK_NAME"
echo "============================================================"

# Read function name and file path from test.sh
FUNC_NAME=$(grep -- '--function' "$TASK_DIR/tests/test.sh" | sed 's/.*--function "\([^"]*\)".*/\1/')
FILE_PATH=$(grep -- '--file-path' "$TASK_DIR/tests/test.sh" | sed 's/.*--file-path "\([^"]*\)".*/\1/')

# Read pre-optimization commit from Dockerfile
COMMIT=$(grep 'git checkout' "$TASK_DIR/environment/Dockerfile" 2>/dev/null | grep -o '[0-9a-f]\{10,\}' | head -1)
if [ -z "$COMMIT" ]; then
    COMMIT="HEAD"
fi

echo "Function: $FUNC_NAME"
echo "File: $FILE_PATH"
echo "Commit: $COMMIT"
echo ""

# Run solve + evaluate in single base container (no per-task image build)
docker run --rm \
    -v "$TASK_DIR/solution:/solution:ro" \
    -v "$TASK_DIR/tests:/tests:ro" \
    "$BASE_IMAGE" \
    bash -c "
        cd /workspace/inference

        # --- SETUP: Get to pre-optimization state ---
        if git cat-file -t $COMMIT >/dev/null 2>&1; then
            git checkout -f $COMMIT 2>&1
        else
            echo 'Commit $COMMIT not found, reconstructing from original.py'
            cp /tests/codeflash_eval/original.py /workspace/inference/$FILE_PATH
            git add -A && git commit -m 'Pre-optimization state (reconstructed)' 2>&1
        fi

        # --- SOLVE ---
        echo '--- SOLVE ---'
        if [ -f /solution/optimization.patch ]; then
            git apply /solution/optimization.patch 2>&1
        else
            python /solution/apply_optimization.py 2>&1
        fi
        git add -A && git commit --allow-empty -m 'Apply optimization' 2>&1
        echo ''

        # --- EVALUATE ---
        echo '--- EVALUATE ---'
        mkdir -p /logs/verifier
        python /tests/codeflash_eval/evaluate.py \\
            --function '$FUNC_NAME' \\
            --file-path '$FILE_PATH' \\
            --repo-path /workspace/inference \\
            --test-dir /tests/codeflash_eval \\
            --output-dir /logs/verifier \\
            $DEBUG_FLAG

        echo ''
        echo '--- reward.json ---'
        cat /logs/verifier/reward.json 2>/dev/null || echo 'NO REWARD FILE'
    "

echo ""
echo "============================================================"
echo "Done: $TASK_NAME"
echo "============================================================"
