#!/bin/bash
# 数据目录结构迁移脚本
# 将现有数据迁移到新的目录结构

set -e

# 获取脚本所在目录
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"

# 颜色定义
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

cd "$PROJECT_ROOT"

echo "=========================================="
echo "数据目录结构迁移脚本"
echo "=========================================="
echo ""

# 账号名称
ACCOUNT_NAME=${1:-"阿里多多酱"}

ACCOUNT_DIR="data/账号/$ACCOUNT_NAME"

if [ ! -d "$ACCOUNT_DIR" ]; then
    echo "错误: 账号目录不存在: $ACCOUNT_DIR"
    exit 1
fi

echo "正在迁移账号: $ACCOUNT_NAME"
echo "账号目录: $ACCOUNT_DIR"
echo ""

# 创建新的目录结构
echo "创建新目录结构..."
mkdir -p "$ACCOUNT_DIR/input"
mkdir -p "$ACCOUNT_DIR/output/intermediate"
mkdir -p "$ACCOUNT_DIR/output/how解构结果"
mkdir -p "$ACCOUNT_DIR/output/visualization"

echo -e "${GREEN}✓${NC} 目录结构创建完成"
echo ""

# 迁移数据
echo "迁移数据..."

# 1. 迁移 what解构 (当前帖子)
if [ -d "$ACCOUNT_DIR/what解构" ] && [ ! -L "$ACCOUNT_DIR/what解构" ]; then
    echo "  移动: what解构 -> input/what解构"
    mv "$ACCOUNT_DIR/what解构" "$ACCOUNT_DIR/input/"
fi

# 2. 迁移 what解构_所有帖子 (过去帖子)
if [ -d "$ACCOUNT_DIR/what解构_所有帖子" ] && [ ! -L "$ACCOUNT_DIR/what解构_所有帖子" ]; then
    echo "  移动: what解构_所有帖子 -> input/what解构_所有帖子"
    mv "$ACCOUNT_DIR/what解构_所有帖子" "$ACCOUNT_DIR/input/"
fi

# 3. pattern 目录已经在正确位置，不需要移动

echo -e "${GREEN}✓${NC} 数据迁移完成"
echo ""

# 显示新的目录结构
echo "新的目录结构:"
echo "=========================================="
tree -L 3 -d "$ACCOUNT_DIR" 2>/dev/null || {
    echo "$ACCOUNT_DIR/"
    echo "├── input/"
    echo "│   ├── what解构/"
    echo "│   └── what解构_所有帖子/"
    echo "├── pattern/"
    echo "│   └── cluster/"
    echo "└── output/"
    echo "    ├── intermediate/"
    echo "    ├── how解构结果/"
    echo "    └── visualization/"
}
echo "=========================================="
echo ""

echo -e "${GREEN}✓${NC} 迁移完成！"
echo ""
echo "提示："
echo "  - 原始数据现在在: $ACCOUNT_DIR/input/"
echo "  - 输出结果将保存到: $ACCOUNT_DIR/output/"
echo "  - pattern数据保持在: $ACCOUNT_DIR/pattern/"
