#!/bin/bash
# =============================================================================
# 步骤 2：将指定舰船的 kg_snapshot.json 导入迁移目标机的 Neo4j（按舰版）
# 在【迁移目标机】上执行本脚本
# 配置项（IMPORT_SHIP 等）请修改 migration_config.sh
# =============================================================================

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
source "${SCRIPT_DIR}/migration_config.sh"

PYTHON="python3"

set -e

if [ -z "$IMPORT_SHIP" ]; then
    echo "错误：migration_config.sh 中 IMPORT_SHIP 未设置。"
    echo "如需导入全量快照，请使用 02_load_neo4j.sh。"
    exit 1
fi

SNAPSHOT_PATH="${DST_CODE_DIR}/migration_split/${IMPORT_SHIP}/kg_snapshot.json"

echo "=========================================="
echo "  kgrag Neo4j 导入脚本 - 步骤 2（按舰版）"
echo "  导入舰船: ${IMPORT_SHIP}"
echo "  快照文件: ${SNAPSHOT_PATH}"
echo "=========================================="

if [ ! -f "${SNAPSHOT_PATH}" ]; then
    echo "错误：快照文件不存在: ${SNAPSHOT_PATH}"
    echo "请先运行 split_by_ship.py 生成按舰快照。"
    exit 1
fi

echo ""
echo "快照文件大小: $(du -sh ${SNAPSHOT_PATH} | cut -f1)"
echo ""

cd "${DST_CODE_DIR}"
${PYTHON} batch_kg_build.py \
    --phase load_snapshot \
    --snapshot_path "${SNAPSHOT_PATH}"

echo ""
echo "=========================================="
echo "  Neo4j 导入完成！已导入: ${IMPORT_SHIP}"
echo "  下一步：执行 bash 02b_rebuild_index.sh"
echo "=========================================="
