# 小虾 SaaS 项目推进器数据初始化脚本
# 将当前项目状态、规则、待办事项录入推进器

$API_BASE = "http://47.98.113.167:8089/api/v1"
$PROJECT_ID = "00000000-0000-0000-0000-000000000001"
$WORKSPACE_ID = "00000000-0000-0000-0000-000000000001"

function Create-Milestone {
    param(
        [string]$Title,
        [string]$TargetDate,
        [string]$Description = ""
    )
    
    $payload = @{
        title = $Title
        target_date = $TargetDate
        project_id = $PROJECT_ID
        workspace_id = $WORKSPACE_ID
        description = $Description
    } | ConvertTo-Json
    
    try {
        $response = Invoke-RestMethod -Uri "$API_BASE/project-management/milestones" -Method Post -Body $payload -ContentType "application/json"
        Write-Host "✅ 里程碑创建成功: $Title" -ForegroundColor Green
        return $response
    } catch {
        Write-Host "❌ 里程碑创建失败: $Title - $($_.Exception.Message)" -ForegroundColor Red
        return $null
    }
}

function Create-Task {
    param(
        [string]$Title,
        [string]$Description,
        [string]$Priority = "MEDIUM",
        [string]$Status = "PENDING",
        [int]$Progress = 0,
        [string[]]$Tags = @()
    )
    
    $payload = @{
        title = $Title
        description = $Description
        project_id = $PROJECT_ID
        workspace_id = $WORKSPACE_ID
        priority = $Priority
        status = $Status
        progress = $Progress
        tags = $Tags
    } | ConvertTo-Json
    
    try {
        $response = Invoke-RestMethod -Uri "$API_BASE/project-management/tasks" -Method Post -Body $payload -ContentType "application/json"
        Write-Host "✅ 任务创建成功: $Title" -ForegroundColor Green
        return $response
    } catch {
        Write-Host "❌ 任务创建失败: $Title - $($_.Exception.Message)" -ForegroundColor Red
        return $null
    }
}

Write-Host "============================================================" -ForegroundColor Cyan
Write-Host "小虾 SaaS 项目推进器 - 数据初始化" -ForegroundColor Cyan
Write-Host "============================================================" -ForegroundColor Cyan

# 创建里程碑
Write-Host "`n📍 创建里程碑..." -ForegroundColor Yellow

Create-Milestone -Title "Phase 1: 核心平台层完成" -TargetDate "2026-06-15" -Description "Clean Architecture 骨架 + 核心业务对象 + 完整测试"
Create-Milestone -Title "Phase 2: 项目管理模块落地" -TargetDate "2026-06-16" -Description "任务/里程碑/问题管理 + 前后端完整链路"
Create-Milestone -Title "Phase 3: 部署与备案完成" -TargetDate "2026-06-30" -Description "生产环境部署 + HTTPS 证书 + 域名备案通过"
Create-Milestone -Title "Phase 4: SAAS 产品化完成" -TargetDate "2026-07-15" -Description "多租户 + 权限体系 + 订阅计费"
Create-Milestone -Title "Phase 5: AI 剪辑能力接入" -TargetDate "2026-08-01" -Description "视频分类模型 + 自动剪辑 + 配音合成"

# 已完成任务
Write-Host "`n✅ 录入已完成任务..." -ForegroundColor Yellow

Create-Task -Title "Clean Architecture 架构设计" -Description "Domain → Ports → Application → Adapters 分层完成" -Status "COMPLETED" -Progress 100 -Priority "URGENT" -Tags @("架构", "Phase1")
Create-Task -Title "核心业务对象实现" -Description "User/Workspace/Project/AssetLibrary/Asset/IngestJob/ClassificationJob 全部完成" -Status "COMPLETED" -Progress 100 -Priority "HIGH" -Tags @("业务逻辑", "Phase1")
Create-Task -Title "双持久化实现" -Description "In-Memory (测试) + SQLAlchemy (生产) 完成" -Status "COMPLETED" -Progress 100 -Priority "HIGH" -Tags @("数据库", "Phase1")
Create-Task -Title "项目管理模块开发" -Description "任务/里程碑/问题 三大实体 + 11 个 Use Cases + 11 个 API" -Status "COMPLETED" -Progress 100 -Priority "URGENT" -Tags @("项目管理", "Phase2")
Create-Task -Title "项目推进器前端开发" -Description "5 个页面 + 3 个表单 + 完整前后端集成" -Status "COMPLETED" -Progress 100 -Priority "HIGH" -Tags @("前端", "Phase2")
Create-Task -Title "Docker Compose 部署" -Description "5 个容器 (postgres/redis/api/worker/web) 全部运行" -Status "COMPLETED" -Progress 100 -Priority "HIGH" -Tags @("部署", "Phase3")
Create-Task -Title "Nginx 反向代理配置" -Description "8088/8089 临时端口配置完成，绕过备案限制" -Status "COMPLETED" -Progress 100 -Priority "MEDIUM" -Tags @("部署", "Phase3")

# 进行中任务
Write-Host "`n🔄 录入进行中任务..." -ForegroundColor Yellow

Create-Task -Title "域名备案审核" -Description "saas.xiaoxiajianji.com / saas-api.xiaoxiajianji.com 等待工信部审核通过" -Status "IN_PROGRESS" -Progress 50 -Priority "URGENT" -Tags @("部署", "Phase3", "阻塞")
Create-Task -Title "HTTPS 证书申请" -Description "Let's Encrypt 证书，等待备案通过后申请" -Status "BLOCKED" -Progress 0 -Priority "HIGH" -Tags @("部署", "Phase3", "依赖备案")

# 待办任务
Write-Host "`n📋 录入待办任务..." -ForegroundColor Yellow

Create-Task -Title "切换到正式域名和 HTTPS" -Description "备案通过后：改回 80/443 端口 + 申请证书 + nginx 配置 HTTPS" -Status "PENDING" -Progress 0 -Priority "HIGH" -Tags @("部署", "Phase3", "依赖备案")
Create-Task -Title "PostgreSQL 生产环境切换" -Description "当前用 In-Memory，需切换到 PostgreSQL + 数据持久化验证" -Status "PENDING" -Progress 0 -Priority "HIGH" -Tags @("数据库", "Phase3")
Create-Task -Title "前端环境变量配置" -Description "API 地址从临时端口改为正式域名 https://saas-api.xiaoxiajianji.com" -Status "PENDING" -Progress 0 -Priority "MEDIUM" -Tags @("前端", "Phase3", "依赖备案")
Create-Task -Title "甘特图视图开发" -Description "项目推进器增加甘特图/时间线视图，可视化任务时间规划" -Status "PENDING" -Progress 0 -Priority "MEDIUM" -Tags @("前端", "Phase2")
Create-Task -Title "批量操作 API" -Description "任务批量更新状态/优先级/删除接口" -Status "PENDING" -Progress 0 -Priority "LOW" -Tags @("后端", "Phase2")
Create-Task -Title "数据导出功能" -Description "导出任务列表为 Excel/CSV" -Status "PENDING" -Progress 0 -Priority "LOW" -Tags @("功能", "Phase2")
Create-Task -Title "多租户权限体系" -Description "Workspace 级别权限控制 + 用户角色管理" -Status "PENDING" -Progress 0 -Priority "URGENT" -Tags @("权限", "Phase4")
Create-Task -Title "认证与账号体系" -Description "JWT 登录 + 注册 + 密码重置" -Status "PENDING" -Progress 0 -Priority "URGENT" -Tags @("认证", "Phase4")
Create-Task -Title "订阅与计费体系" -Description "SaaS 订阅套餐 + 支付接入（微信/支付宝）" -Status "PENDING" -Progress 0 -Priority "HIGH" -Tags @("商业化", "Phase4")
Create-Task -Title "视频分类模型接入" -Description "真实 AI 模型替换占位分类逻辑" -Status "PENDING" -Progress 0 -Priority "URGENT" -Tags @("AI", "Phase5")
Create-Task -Title "自动剪辑能力" -Description "视频自动剪辑 + 转场特效 + 字幕生成" -Status "PENDING" -Progress 0 -Priority "HIGH" -Tags @("AI", "Phase5")
Create-Task -Title "配音合成能力" -Description "AI 配音 + 音频混音" -Status "PENDING" -Progress 0 -Priority "MEDIUM" -Tags @("AI", "Phase5")

Write-Host "`n============================================================" -ForegroundColor Cyan
Write-Host "✅ 数据初始化完成！" -ForegroundColor Green
Write-Host "============================================================" -ForegroundColor Cyan
Write-Host "`n访问推进器: http://47.98.113.167:8088/projects" -ForegroundColor Cyan
Write-Host "访问 API 文档: http://47.98.113.167:8089/docs`n" -ForegroundColor Cyan
