#!/usr/bin/env bash
# Action Recognition 训练 — 基于 SubPattern 靶向生成的 AR30 题
#
# 三阶段:
#   Phase 0: baseline infer (AR30 题 + VME benchmark 作为 test)
#   Phase 1: create seed (ar30-baseline)
#   Phase 2: train (3 epochs, per_category)
#
# 用法:
#   CUDA_VISIBLE_DEVICES=0 bash scripts/train_ar30.sh
#   MODE=mock bash scripts/train_ar30.sh   # 跳过 Phase 0/1
#
# 与上次训练的区别:
#   - 题目来源: generated-ar30（SubPattern 靶向）替代 generated-v2-360（OCR 污染）
#   - seed 名: ar30-baseline（独立于旧的 v2ar-baseline）
#   - workspace: workspaces/train-ar30

set -euo pipefail

cd "$(dirname "$0")/.."

CUDA_VISIBLE_DEVICES="${CUDA_VISIBLE_DEVICES:-0}"
export CUDA_VISIBLE_DEVICES

export HF_HUB_OFFLINE=1
export TRANSFORMERS_OFFLINE=1
export PYTHONUNBUFFERED=1

set -a
source .env
set +a

PYTHON="$(conda run -n Video-Tree-TRM which python)"

# ── Phase 0: Baseline infer（用 AR30 新题跑基线推理）──
if [[ "${MODE:-}" != "mock" ]]; then
    echo "=== Phase 0: Baseline infer (AR30 新题 30 题) ==="
    "${PYTHON}" main.py \
        --config config/train_ar30.yaml \
        --workspace-dir workspaces/default \
        --store-dir store \
        --mode infer \
        --concurrency 24 \
        --max-steps 40 \
        --skill-mode auto \
        --n-samples 0 \
        --questions "generated-ar30" \
        --skills-version v1 \
        --prompts-version v1 \
        --run-id ar30_baseline \
        --task-types "Action Recognition"
fi

# ── Phase 1: Create seed ──
if [[ "${MODE:-}" != "mock" && ! -d "store/seeds/ar30-baseline" ]]; then
    echo "=== Phase 1: Create seed ar30-baseline ==="
    "${PYTHON}" -c "
from pathlib import Path
from app.harness.store import extract_run_db, init_seed
import tempfile

tmp = Path(tempfile.mkdtemp()) / 'baseline.db'
extract_run_db(
    Path('workspaces/default/harness.db'),
    tmp,
    'infer_ar30_baseline',
)
init_seed(
    store_dir=Path('store'),
    name='ar30-baseline',
    skills_dir=Path('store/skills/v1'),
    prompts_dir=Path('store/prompts/v1'),
    baseline_db=tmp,
    baseline_run_id='infer_ar30_baseline',
    parent=None,
    description='AR30 SubPattern 靶向题 baseline (skills/v1)',
)
tmp.unlink()
print('Seed created: store/seeds/ar30-baseline/')
"
elif [[ -d "store/seeds/ar30-baseline" ]]; then
    echo "=== Phase 1: Seed ar30-baseline 已存在，跳过 ==="
fi

# ── Phase 2: Train ──
echo "=== Phase 2: Train (3 epochs, AR30) ==="
"${PYTHON}" main.py \
    --config config/train_ar30.yaml \
    --fresh \
    --seed ar30-baseline

echo "=== 训练完成 ==="
echo "结果查看:"
echo "  cat workspaces/train-ar30/analyses/final_test_eval.json"
echo "  sqlite3 workspaces/train-ar30/harness.db 'SELECT * FROM dual_metric'"
