#!/usr/bin/env bash
# Run full pipeline for a mission: stages 02→03→04→05
# Usage: ./run_pipeline.sh <mission> [worker]
# Example: ./run_pipeline.sh 20260505-Lepradet auto

set -euo pipefail

MISSION=${1:-20260505-Lepradet}
WORKER=${2:-auto}
MANIFEST="/home/cosma/cosma-pipeline/${MISSION}/manifest.json"
PIPELINE_DIR="$(cd "$(dirname "$0")" && pwd)/stages"
PIPELINE_BASE="/home/cosma/cosma-pipeline"
NAV_DIR="${PIPELINE_BASE}/data/${MISSION}/nav"
NAV_FILT_DIR="${PIPELINE_BASE}/data/${MISSION}/nav_filtered"
FRAMES_DIR="${PIPELINE_BASE}/data/${MISSION}/frames"

RUN_ID="$(date +%Y%m%d_%H%M%S)"
RUN_LOG_DIR="${PIPELINE_BASE}/runs/${RUN_ID}"
mkdir -p "${RUN_LOG_DIR}"

echo "=== Pipeline run ${RUN_ID} mission=${MISSION} worker=${WORKER} ===" | tee "${RUN_LOG_DIR}/run.log"
echo "Start: $(date -u +%Y-%m-%dT%H:%M:%SZ)" | tee -a "${RUN_LOG_DIR}/run.log"

# Stage 02: nav parse
echo "" | tee -a "${RUN_LOG_DIR}/run.log"
echo "--- Stage 02: nav parse ---" | tee -a "${RUN_LOG_DIR}/run.log"
python3 "${PIPELINE_DIR}/02_nav_parse.py" "${MANIFEST}" \
    2>&1 | tee -a "${RUN_LOG_DIR}/stage02.log" "${RUN_LOG_DIR}/run.log"

# Stage 03: nav filter
echo "" | tee -a "${RUN_LOG_DIR}/run.log"
echo "--- Stage 03: nav filter ---" | tee -a "${RUN_LOG_DIR}/run.log"
python3 "${PIPELINE_DIR}/03_nav_filter.py" "${NAV_DIR}" \
    2>&1 | tee -a "${RUN_LOG_DIR}/stage03.log" "${RUN_LOG_DIR}/run.log"

# QC threshold: lowered from 50 to 30 (avg bottom_visible=37.5%)
export COSMA_QC_BOTTOM_OK_PCT=30

# Stage 04: frame extract
echo "" | tee -a "${RUN_LOG_DIR}/run.log"
echo "--- Stage 04: frame extract ---" | tee -a "${RUN_LOG_DIR}/run.log"
python3 "${PIPELINE_DIR}/04_frame_extract.py" --mission "${MISSION}" \
    2>&1 | tee -a "${RUN_LOG_DIR}/stage04.log" "${RUN_LOG_DIR}/run.log"

# Stage 05: inference (sequential, one segment at a time)
echo "" | tee -a "${RUN_LOG_DIR}/run.log"
echo "--- Stage 05: inference ---" | tee -a "${RUN_LOG_DIR}/run.log"
python3 "${PIPELINE_DIR}/05_inference.py" \
    --frames-dir "${FRAMES_DIR}" \
    --worker "${WORKER}" \
    --mission "${MISSION}" \
    2>&1 | tee -a "${RUN_LOG_DIR}/stage05.log" "${RUN_LOG_DIR}/run.log"

echo "" | tee -a "${RUN_LOG_DIR}/run.log"
echo "=== Pipeline DONE $(date -u +%Y-%m-%dT%H:%M:%SZ) ===" | tee -a "${RUN_LOG_DIR}/run.log"
echo "Logs: ${RUN_LOG_DIR}/"
