#!/bin/bash
#
# run_mining_data.sh
#
# Script to run online inference-based bad case mining for data distillation.
# This runs step1_2_3_merged_extract.py with online inference enabled.
#

set -euo pipefail

# ============================================================================
# Configuration - Modify these variables as needed
# ============================================================================

# UUID list to process
UUID_LIST="${UUID_LIST:-./data_distillation/group_uuid_list_first_batch.txt}"

# Skip UUID file (previously extracted UUIDs)
SKIP_UUID_FILE="${SKIP_UUID_FILE:-./data_distillation/g1m3_pdcl_total_20260428.txt}"


# Output directory
# OUTPUT_ROOT="${OUTPUT_ROOT:-/mnt/pdcl/tmp/alg_results/Mono3d_Data/G1Q3-large-vehicle-first-batch}"
OUTPUT_ROOT="${OUTPUT_ROOT:-./test_online_output_new}"

# Vehicle name fallback (resolved per-UUID inside the Python script from gt_common metadata)
VEHICLE_NAME="${VEHICLE_NAME:-UNKNOWN}"

# Camera ID
CAMERA_ID="${CAMERA_ID:-camera4}"

# Product type
PRODUCT_TYPE="${PRODUCT_TYPE:-2d-3d-association}"

# ============================================================================
# Online Inference Configuration
# ============================================================================

# ROI model paths
ROI0_MODEL="${ROI0_MODEL:-./mono3d_roi0_20260427_epoch99.pt}"
ROI1_MODEL="${ROI1_MODEL:-./mono3d_roi1_20260427_epoch99.pt}"

# ROI crop sizes (W H)
ROI0_SIZE="${ROI0_SIZE:-1920 880}"
ROI1_SIZE="${ROI1_SIZE:-768 352}"

# Virtual focal lengths
ROI0_VIRTUAL_FX="${ROI0_VIRTUAL_FX:-537.0}"
ROI1_VIRTUAL_FX="${ROI1_VIRTUAL_FX:-537.0}"

# Crop center modes (cxvy or vxvy)
ROI0_CROP_CENTER_MODE="${ROI0_CROP_CENTER_MODE:-cxvy}"
ROI1_CROP_CENTER_MODE="${ROI1_CROP_CENTER_MODE:-vxvy}"

# Confidence thresholds
ROI0_CONF="${ROI0_CONF:-0.25}"
ROI1_CONF="${ROI1_CONF:-0.25}"

# Inference device (GPU ID or 'cpu')
INFERENCE_DEVICE="${INFERENCE_DEVICE:-0}"

# Use FP16 for inference (true/false)
INFERENCE_HALF="${INFERENCE_HALF:-true}"

# Inference batch size
INFERENCE_BATCH_SIZE="${INFERENCE_BATCH_SIZE:-4}"

# ============================================================================
# Mining Parameters
# ============================================================================

# Near bad case thresholds (lateral < 10m AND longitudinal < 50m)
NEAR_YAW_THRESHOLD_DEG="${NEAR_YAW_THRESHOLD_DEG:-3.0}"
NEAR_DISTANCE_THRESHOLD_PCT="${NEAR_DISTANCE_THRESHOLD_PCT:-3.0}"

# Far bad case thresholds (lateral < 5m AND longitudinal > 50m)
FAR_YAW_THRESHOLD_DEG="${FAR_YAW_THRESHOLD_DEG:-5.0}"
FAR_DISTANCE_THRESHOLD_PCT="${FAR_DISTANCE_THRESHOLD_PCT:-5.0}"

# Sequence-based mining parameters
SEQUENCE_LENGTH="${SEQUENCE_LENGTH:-10}"
MIN_BAD_FRAMES="${MIN_BAD_FRAMES:-3}"

# Edge yaw max lateral distance for edge yaw selection
EDGE_YAW_MAX_LATERAL_DIST="${EDGE_YAW_MAX_LATERAL_DIST:-30.0}"

# ============================================================================
# Processing Options
# ============================================================================

# Number of parallel workers (online inference requires 1)
WORKERS="${WORKERS:-1}"

# Max UUIDs to process (0 = all)
MAX_UUIDS="${MAX_UUIDS:-0}"

# Valid stride (keep every Nth annotation)
VALID_STRIDE="${VALID_STRIDE:-1}"

# JPEG quality for extracted images
IMAGE_JPEG_QUALITY="${IMAGE_JPEG_QUALITY:-95}"

# Visualization options
VIS_MODE="${VIS_MODE:-split}"
VIS_SCALE="${VIS_SCALE:-0.5}"
VIS_SAMPLES_PER_UUID="${VIS_SAMPLES_PER_UUID:-0}"
VIS_DIR_NAME="${VIS_DIR_NAME:-show_samples}"

# Skip options (set to "true" to enable)
NO_IMAGES="${NO_IMAGES:-false}"
NO_LABELS="${NO_LABELS:-false}"
NO_ANNOTATIONS="${NO_ANNOTATIONS:-false}"
NO_VIS_SAMPLES="${NO_VIS_SAMPLES:-false}"

# Overwrite existing files
OVERWRITE="${OVERWRITE:-false}"

# Dry run (scan only, don't write)
DRY_RUN="${DRY_RUN:-false}"

# ============================================================================
# Script - No need to modify below
# ============================================================================

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
EXTRACT_SCRIPT="${SCRIPT_DIR}/data_distillation/step1_2_3_merged_extract.py"

# Check if extract script exists
if [ ! -f "${EXTRACT_SCRIPT}" ]; then
    echo "ERROR: Extract script not found: ${EXTRACT_SCRIPT}"
    exit 1
fi

# Check if UUID list exists
if [ ! -f "${UUID_LIST}" ]; then
    echo "ERROR: UUID list not found: ${UUID_LIST}"
    exit 1
fi

# Check if ROI models exist
if [ ! -f "${ROI0_MODEL}" ]; then
    echo "ERROR: ROI0 model not found: ${ROI0_MODEL}"
    exit 1
fi

if [ ! -f "${ROI1_MODEL}" ]; then
    echo "ERROR: ROI1 model not found: ${ROI1_MODEL}"
    exit 1
fi

# Build command
CMD=(
    python "${EXTRACT_SCRIPT}"
    --uuid-list "${UUID_LIST}"
    --skip-uuid-file "${SKIP_UUID_FILE}"
    --output-root "${OUTPUT_ROOT}"
    --default-vehicle-name "${VEHICLE_NAME}"
    --camera-id "${CAMERA_ID}"
    --product-type "${PRODUCT_TYPE}"
    --workers "${WORKERS}"
    --max-uuids "${MAX_UUIDS}"
    --valid-stride "${VALID_STRIDE}"
    --image-jpeg-quality "${IMAGE_JPEG_QUALITY}"
    --vis-mode "${VIS_MODE}"
    --vis-scale "${VIS_SCALE}"
    --vis-samples-per-uuid "${VIS_SAMPLES_PER_UUID}"
    --vis-dir-name "${VIS_DIR_NAME}"
    --sequence-length "${SEQUENCE_LENGTH}"
    --min-bad-frames "${MIN_BAD_FRAMES}"
    --edge-yaw-max-lateral-dist "${EDGE_YAW_MAX_LATERAL_DIST}"
    --near-yaw-threshold "${NEAR_YAW_THRESHOLD_DEG}"
    --near-distance-threshold "${NEAR_DISTANCE_THRESHOLD_PCT}"
    --far-yaw-threshold "${FAR_YAW_THRESHOLD_DEG}"
    --far-distance-threshold "${FAR_DISTANCE_THRESHOLD_PCT}"
    --roi0-model "${ROI0_MODEL}"
    --roi1-model "${ROI1_MODEL}"
    --roi0-size ${ROI0_SIZE}
    --roi1-size ${ROI1_SIZE}
    --roi0-virtual-fx "${ROI0_VIRTUAL_FX}"
    --roi1-virtual-fx "${ROI1_VIRTUAL_FX}"
    --roi0-crop-center-mode "${ROI0_CROP_CENTER_MODE}"
    --roi1-crop-center-mode "${ROI1_CROP_CENTER_MODE}"
    --roi0-conf "${ROI0_CONF}"
    --roi1-conf "${ROI1_CONF}"
    --inference-device "${INFERENCE_DEVICE}"
    --inference-batch-size "${INFERENCE_BATCH_SIZE}"
)

# Add optional flags
if [ "${NO_IMAGES}" = "true" ]; then
    CMD+=(--no-images)
fi

if [ "${NO_LABELS}" = "true" ]; then
    CMD+=(--no-labels)
fi

if [ "${NO_ANNOTATIONS}" = "true" ]; then
    CMD+=(--no-annotations)
fi

if [ "${NO_VIS_SAMPLES}" = "true" ]; then
    CMD+=(--no-vis-samples)
fi

if [ "${OVERWRITE}" = "true" ]; then
    CMD+=(--overwrite)
fi

if [ "${INFERENCE_HALF}" = "true" ]; then
    CMD+=(--inference-half)
else
    CMD+=(--no-inference-half)
fi

if [ "${DRY_RUN}" = "true" ]; then
    CMD+=(--dry-run)
fi

# Print configuration
echo "=========================================================================="
echo "Online Inference Bad Case Mining"
echo "=========================================================================="
echo "UUID List              : ${UUID_LIST}"
echo "Skip UUID File         : ${SKIP_UUID_FILE}"
echo "Output Root            : ${OUTPUT_ROOT}"
echo "Vehicle Name           : ${VEHICLE_NAME}"
echo "Camera ID              : ${CAMERA_ID}"
echo ""
echo "ROI0 Model             : ${ROI0_MODEL}"
echo "ROI1 Model             : ${ROI1_MODEL}"
echo "Inference Device       : ${INFERENCE_DEVICE}"
echo "Inference Half         : ${INFERENCE_HALF}"
echo ""
echo "Sequence Length        : ${SEQUENCE_LENGTH}"
echo "Min Bad Frames         : ${MIN_BAD_FRAMES}"
echo "Near Yaw Threshold     : ${NEAR_YAW_THRESHOLD_DEG}°"
echo "Near Distance Thres    : ${NEAR_DISTANCE_THRESHOLD_PCT}%"
echo "Far Yaw Threshold      : ${FAR_YAW_THRESHOLD_DEG}°"
echo "Far Distance Thres     : ${FAR_DISTANCE_THRESHOLD_PCT}%"
echo ""
echo "Workers                : ${WORKERS}"
echo "Max UUIDs              : ${MAX_UUIDS} (0 = all)"
echo "Valid Stride           : ${VALID_STRIDE}"
echo "=========================================================================="
echo ""

# Run the command
echo "Running: ${CMD[*]}"
echo ""

exec "${CMD[@]}"
