#!/usr/bin/env bash
# 从 TRM4.zip 迁移资产到 TRM5
# 用法: bash tools/migrate_from_trm4.sh /path/to/Video-Tree-TRM4.zip
set -euo pipefail

ZIP_PATH="${1:?用法: bash tools/migrate_from_trm4.sh /path/to/Video-Tree-TRM4.zip}"
PROJECT_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
TMP_DIR=$(mktemp -d)

echo "=== TRM4 -> TRM5 迁移 ==="
echo "ZIP: $ZIP_PATH"
echo "项目根: $PROJECT_ROOT"
echo "临时目录: $TMP_DIR"

# 1. 解压
echo "[1/6] 解压 TRM4.zip..."
unzip -q "$ZIP_PATH" -d "$TMP_DIR"
SRC="$TMP_DIR/Video-Tree-TRM4"

# 2. 拷贝帧文件 (rsync --ignore-existing)
echo "[2/6] 拷贝帧文件..."
mkdir -p "$PROJECT_ROOT/store/videos"
for vid_dir in "$SRC"/store/videos/*/; do
    vid=$(basename "$vid_dir")
    dst="$PROJECT_ROOT/store/videos/$vid"
    mkdir -p "$dst"
    if [ -d "$vid_dir/frames" ]; then
        rsync -a --ignore-existing "$vid_dir/frames/" "$dst/frames/"
    fi
done

# 3. 拷贝 SRT 字幕
echo "[3/6] 拷贝 SRT 字幕..."
mkdir -p "$PROJECT_ROOT/data/Video-MME/subtitle"
if [ -d "$SRC/data/Video-MME/subtitle" ]; then
    rsync -a --ignore-existing "$SRC/data/Video-MME/subtitle/" "$PROJECT_ROOT/data/Video-MME/subtitle/"
fi

# 4. 拷贝视频压缩包
echo "[4/6] 拷贝视频压缩包..."
mkdir -p "$PROJECT_ROOT/data/Video-MME/original_data"
if [ -d "$SRC/data/Video-MME/original_data" ]; then
    rsync -a --ignore-existing "$SRC/data/Video-MME/original_data/" "$PROJECT_ROOT/data/Video-MME/original_data/"
fi

# 5. 拷贝问题 JSON
echo "[5/6] 拷贝 Benchmark 问题..."
mkdir -p "$PROJECT_ROOT/store/questions"
if [ -d "$SRC/store/questions" ]; then
    rsync -a --ignore-existing "$SRC/store/questions/" "$PROJECT_ROOT/store/questions/"
fi

# 6. 格式转换
echo "[6/6] 格式转换 flat -> TreeIndex..."
conda run -n Video-Tree-TRM python "$PROJECT_ROOT/tools/convert_flat_to_treeindex.py" \
    "$SRC/store/videos" "$PROJECT_ROOT/store/videos"

# 验收
echo ""
echo "=== 验收检查 ==="
VIDEO_COUNT=$(find "$PROJECT_ROOT/store/videos" -name "tree.json" | wc -l)
SRT_COUNT=$(find "$PROJECT_ROOT/data/Video-MME/subtitle" -name "*.srt" 2>/dev/null | wc -l)
echo "视频树: $VIDEO_COUNT (期望 300)"
echo "SRT 字幕: $SRT_COUNT (期望 >=290)"

# 清理
echo "清理临时目录..."
rm -rf "$TMP_DIR"

if [ "$VIDEO_COUNT" -lt 300 ]; then
    echo "WARNING: 视频树数量不足 300，请检查"
    exit 1
fi

echo "迁移完成"
