#!/bin/bash
# doc_mall 项目迁移脚本 (Bash)
# 用途: 将 doc_mall 模块迁移到新仓库
# 使用: ./migrate.sh /path/to/new-repo

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color

# 参数检查
if [ $# -eq 0 ]; then
    echo -e "${RED}错误: 请提供目标路径${NC}"
    echo "用法: $0 <目标路径> [选项]"
    echo "选项:"
    echo "  --copy-supabase    复制 Supabase 组件"
    echo "  --copy-utils       复制工具函数"
    echo "  --dry-run          预览模式，不实际复制"
    exit 1
fi

TARGET_PATH="$1"
SOURCE_PATH="${2:-.}"
COPY_SUPABASE=false
COPY_UTILS=false
DRY_RUN=false

# 解析选项
shift
while [[ $# -gt 0 ]]; do
    case $1 in
        --copy-supabase)
            COPY_SUPABASE=true
            shift
            ;;
        --copy-utils)
            COPY_UTILS=true
            shift
            ;;
        --dry-run)
            DRY_RUN=true
            shift
            ;;
        *)
            echo -e "${RED}未知选项: $1${NC}"
            exit 1
            ;;
    esac
done

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

echo -e "${CYAN}===========================================${NC}"
echo -e "${CYAN}  doc_mall 项目迁移脚本${NC}"
echo -e "${CYAN}===========================================${NC}"
echo ""
echo -e "${GREEN}源项目路径: $SOURCE_ROOT${NC}"
echo -e "${GREEN}目标项目路径: $TARGET_PATH${NC}"
echo ""

# 创建目标目录结构
DIRECTORIES=(
    "$TARGET_PATH/doc_mall/analysis"
    "$TARGET_PATH/doc_mall/database"
    "$TARGET_PATH/doc_mall/reports"
    "$TARGET_PATH/pages/mall"
    "$TARGET_PATH/types"
    "$TARGET_PATH/components/supadb"
    "$TARGET_PATH/utils"
)

echo -e "${YELLOW}创建目标目录结构...${NC}"
for dir in "${DIRECTORIES[@]}"; do
    if [ ! -d "$dir" ]; then
        if [ "$DRY_RUN" = true ]; then
            echo -e "  ${YELLOW}[DRY RUN]${NC} 将创建: $dir"
        else
            mkdir -p "$dir"
            echo -e "  ${GREEN}✓${NC} 已创建: $dir"
        fi
    else
        echo -e "  - 已存在: $dir"
    fi
done
echo ""

# 复制函数
copy_item() {
    local source="$1"
    local target="$2"
    local description="$3"
    
    echo -e "${CYAN}处理: $description${NC}"
    
    if [ ! -e "$source" ]; then
        echo -e "  ${RED}✗${NC} 源路径不存在: $source"
        return 1
    fi
    
    if [ "$DRY_RUN" = true ]; then
        if [ -f "$source" ]; then
            echo -e "  ${YELLOW}[DRY RUN]${NC} 将复制文件: $source -> $target"
        else
            file_count=$(find "$source" -type f 2>/dev/null | wc -l)
            echo -e "  ${YELLOW}[DRY RUN]${NC} 将复制目录: $source -> $target"
            echo -e "    预计复制 $file_count 个文件"
        fi
        return 0
    fi
    
    if [ -f "$source" ]; then
        target_dir=$(dirname "$target")
        mkdir -p "$target_dir"
        cp "$source" "$target"
        echo -e "  ${GREEN}✓${NC} 已复制文件"
    else
        file_count=$(find "$source" -type f 2>/dev/null | wc -l)
        cp -r "$source" "$target"
        echo -e "  ${GREEN}✓${NC} 已复制目录: $file_count 个文件"
    fi
    echo ""
}

# 复制必需文件
echo -e "${YELLOW}开始复制文件...${NC}"
echo ""

copy_item "$SOURCE_ROOT/doc_mall" "$TARGET_PATH/doc_mall" "doc_mall 文档目录"
copy_item "$SOURCE_ROOT/pages/mall" "$TARGET_PATH/pages/mall" "pages/mall 页面代码"
copy_item "$SOURCE_ROOT/types/mall-types.uts" "$TARGET_PATH/types/mall-types.uts" "类型定义文件"

# 可选复制项
if [ "$COPY_SUPABASE" = true ]; then
    copy_item "$SOURCE_ROOT/components/supadb" "$TARGET_PATH/components/supadb" "Supabase 客户端组件"
fi

if [ "$COPY_UTILS" = true ]; then
    echo -e "${YELLOW}检查需要的工具函数...${NC}"
    # 查找 pages/mall 中的 utils 引用
    if [ -d "$SOURCE_ROOT/pages/mall" ]; then
        grep -r "from '@/utils" "$SOURCE_ROOT/pages/mall" --include="*.uvue" | \
            sed -n "s/.*from '\(@\/utils\/[^']*\)'.*/\1/p" | \
            sort -u | while read -r util_ref; do
                util_file=$(echo "$util_ref" | sed "s/@\/utils\///")
                util_path="$SOURCE_ROOT/utils/$util_file"
                if [ -f "$util_path" ]; then
                    echo -e "  ${CYAN}发现依赖: utils/$util_file${NC}"
                fi
            done
    fi
    echo ""
fi

echo -e "${CYAN}===========================================${NC}"
if [ "$DRY_RUN" = true ]; then
    echo -e "${YELLOW}  [DRY RUN] 预览完成${NC}"
else
    echo -e "${GREEN}  迁移完成！${NC}"
fi
echo -e "${CYAN}===========================================${NC}"
echo ""

if [ "$DRY_RUN" = false ]; then
    echo -e "${YELLOW}后续步骤:${NC}"
    echo "1. 检查目标目录: $TARGET_PATH"
    echo "2. 更新导入路径（如需要）"
    echo "3. 配置 Supabase 连接信息"
    echo "4. 执行数据库脚本"
    echo "5. 运行测试验证"
    echo ""
    echo -e "${CYAN}详细步骤请参考: doc_mall/MIGRATION_GUIDE.md${NC}"
fi
