# doc_mall 项目迁移脚本 (PowerShell)
# 用途: 将 doc_mall 模块迁移到新仓库
# 使用: .\migrate.ps1 -TargetPath "C:\path\to\new-repo"

param(
  [Parameter(Mandatory = $true)]
  [string]$TargetPath,
    
  [Parameter(Mandatory = $false)]
  [string]$SourcePath = ".",
    
  [Parameter(Mandatory = $false)]
  [switch]$CopySupabaseComponents = $false,
    
  [Parameter(Mandatory = $false)]
  [switch]$CopyUtils = $false,
    
  [Parameter(Mandatory = $false)]
  [switch]$DryRun = $false
)

Write-Host "===========================================" -ForegroundColor Cyan
Write-Host "  doc_mall 项目迁移脚本" -ForegroundColor Cyan
Write-Host "===========================================" -ForegroundColor Cyan
Write-Host ""

# 获取脚本所在目录
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$SourceRoot = Resolve-Path $SourcePath

Write-Host "源项目路径: $SourceRoot" -ForegroundColor Green
Write-Host "目标项目路径: $TargetPath" -ForegroundColor Green
Write-Host ""

# 创建目标目录结构
$Directories = @(
  "$TargetPath\doc_mall\analysis",
  "$TargetPath\doc_mall\database",
  "$TargetPath\doc_mall\reports",
  "$TargetPath\pages\mall",
  "$TargetPath\types",
  "$TargetPath\components\supadb",
  "$TargetPath\utils"
)

Write-Host "创建目标目录结构..." -ForegroundColor Yellow
foreach ($dir in $Directories) {
  if (-not (Test-Path $dir)) {
    if ($DryRun) {
      Write-Host "  [DRY RUN] 将创建: $dir" -ForegroundColor Gray
    }
    else {
      New-Item -ItemType Directory -Path $dir -Force | Out-Null
      Write-Host "  ✓ 已创建: $dir" -ForegroundColor Green
    }
  }
  else {
    Write-Host "  - 已存在: $dir" -ForegroundColor Gray
  }
}
Write-Host ""

# 定义需要复制的文件和目录
$CopyItems = @(
  @{
    Source      = "$SourceRoot\doc_mall\*"
    Target      = "$TargetPath\doc_mall\"
    Description = "doc_mall 文档目录"
  },
  @{
    Source      = "$SourceRoot\pages\mall\*"
    Target      = "$TargetPath\pages\mall\"
    Description = "pages/mall 页面代码"
  },
  @{
    Source      = "$SourceRoot\types\mall-types.uts"
    Target      = "$TargetPath\types\mall-types.uts"
    Description = "类型定义文件"
  }
)

# 可选复制项
if ($CopySupabaseComponents) {
  $CopyItems += @{
    Source      = "$SourceRoot\components\supadb\*"
    Target      = "$TargetPath\components\supadb\"
    Description = "Supabase 客户端组件"
  }
}

if ($CopyUtils) {
  Write-Host "检查需要的工具函数..." -ForegroundColor Yellow
  # 扫描 pages/mall 中的 utils 引用
  $utilsFiles = Get-ChildItem -Path "$SourceRoot\pages\mall" -Recurse -Filter "*.uvue" | 
  Select-String -Pattern "from '@/utils" | 
  ForEach-Object { 
    if ($_.Line -match "from '@/utils/([^']+)'") {
      $matches[1]
    }
  } | Sort-Object -Unique
    
  foreach ($util in $utilsFiles) {
    $utilPath = "$SourceRoot\utils\$util"
    if (Test-Path $utilPath) {
      Write-Host "  发现依赖: utils/$util" -ForegroundColor Cyan
    }
  }
}

Write-Host ""
Write-Host "开始复制文件..." -ForegroundColor Yellow
Write-Host ""

$totalFiles = 0
$copiedFiles = 0
$skippedFiles = 0

foreach ($item in $CopyItems) {
  $source = $item.Source
  $target = $item.Target
  $description = $item.Description
    
  Write-Host "处理: $description" -ForegroundColor Cyan
    
  if (-not (Test-Path $source)) {
    Write-Host "  ✗ 源路径不存在: $source" -ForegroundColor Red
    continue
  }
    
  if ($DryRun) {
    Write-Host "  [DRY RUN] 将复制: $source -> $target" -ForegroundColor Gray
    $files = Get-ChildItem -Path $source -Recurse -File -ErrorAction SilentlyContinue
    $totalFiles += $files.Count
    Write-Host "    预计复制 $($files.Count) 个文件" -ForegroundColor Gray
  }
  else {
    try {
      # 如果是文件
      if (Test-Path $source -PathType Leaf) {
        $targetDir = Split-Path -Parent $target
        if (-not (Test-Path $targetDir)) {
          New-Item -ItemType Directory -Path $targetDir -Force | Out-Null
        }
        Copy-Item -Path $source -Destination $target -Force
        $copiedFiles++
        Write-Host "  ✓ 已复制文件: $source" -ForegroundColor Green
      }
      # 如果是目录
      else {
        $files = Get-ChildItem -Path $source -Recurse -File -ErrorAction SilentlyContinue
        $totalFiles += $files.Count
                
        Copy-Item -Path $source -Destination $target -Recurse -Force -ErrorAction Stop
        $copiedFiles += $files.Count
        Write-Host "  ✓ 已复制目录: $($files.Count) 个文件" -ForegroundColor Green
      }
    }
    catch {
      Write-Host "  ✗ 复制失败: $_" -ForegroundColor Red
      $skippedFiles++
    }
  }
  Write-Host ""
}

Write-Host "===========================================" -ForegroundColor Cyan
if ($DryRun) {
  Write-Host "  [DRY RUN] 预览完成" -ForegroundColor Yellow
  Write-Host "  预计复制文件数: $totalFiles" -ForegroundColor Yellow
}
else {
  Write-Host "  迁移完成！" -ForegroundColor Green
  Write-Host "  已复制文件数: $copiedFiles" -ForegroundColor Green
  if ($skippedFiles -gt 0) {
    Write-Host "  跳过文件数: $skippedFiles" -ForegroundColor Yellow
  }
}
Write-Host "===========================================" -ForegroundColor Cyan
Write-Host ""

if (-not $DryRun) {
  Write-Host "后续步骤:" -ForegroundColor Yellow
  Write-Host "1. 检查目标目录: $TargetPath" -ForegroundColor White
  Write-Host "2. 更新导入路径（如需要）" -ForegroundColor White
  Write-Host "3. 配置 Supabase 连接信息" -ForegroundColor White
  Write-Host "4. 执行数据库脚本" -ForegroundColor White
  Write-Host "5. 运行测试验证" -ForegroundColor White
  Write-Host ""
  Write-Host "详细步骤请参考: doc_mall/MIGRATION_GUIDE.md" -ForegroundColor Cyan
}
