"""
Phase 6: 前端完善 - 任务初始化脚本
将 Phase 6 的所有任务同步到推进器
"""

import sqlite3
from datetime import datetime

# Phase 6 任务列表
PHASE6_TASKS = [
    # Week 1-2: 基础搭建 (7个任务)
    {
        "name": "项目初始化：Vite + React + TypeScript",
        "description": "创建 Vite 项目，配置 React 18 和 TypeScript",
        "phase": "Phase 6",
        "milestone": "Week 1-2: 基础搭建",
        "priority": "high",
        "estimated_hours": 2,
    },
    {
        "name": "安装配置依赖包",
        "description": "安装 Ant Design, React Router, Zustand, React Query, Axios 等",
        "phase": "Phase 6",
        "milestone": "Week 1-2: 基础搭建",
        "priority": "high",
        "estimated_hours": 2,
    },
    {
        "name": "搭建基础布局组件",
        "description": "创建 Header, Sidebar, Footer 等布局组件",
        "phase": "Phase 6",
        "milestone": "Week 1-2: 基础搭建",
        "priority": "high",
        "estimated_hours": 4,
    },
    {
        "name": "API 客户端封装",
        "description": "封装 Axios，配置拦截器和 token 管理",
        "phase": "Phase 6",
        "milestone": "Week 1-2: 基础搭建",
        "priority": "high",
        "estimated_hours": 3,
    },
    {
        "name": "路由配置",
        "description": "配置 React Router，定义所有路由",
        "phase": "Phase 6",
        "milestone": "Week 1-2: 基础搭建",
        "priority": "high",
        "estimated_hours": 2,
    },
    {
        "name": "设计系统配置",
        "description": "配置色彩、字体、间距等 CSS 变量",
        "phase": "Phase 6",
        "milestone": "Week 1-2: 基础搭建",
        "priority": "medium",
        "estimated_hours": 2,
    },
    {
        "name": "TypeScript 类型定义",
        "description": "定义 API 响应、实体等 TypeScript 类型",
        "phase": "Phase 6",
        "milestone": "Week 1-2: 基础搭建",
        "priority": "medium",
        "estimated_hours": 3,
    },
    # Week 3: 认证页面 (5个任务)
    {
        "name": "登录页面",
        "description": "实现用户登录界面和逻辑",
        "phase": "Phase 6",
        "milestone": "Week 3: 认证页面",
        "priority": "high",
        "estimated_hours": 4,
    },
    {
        "name": "注册页面",
        "description": "实现用户注册界面和表单验证",
        "phase": "Phase 6",
        "milestone": "Week 3: 认证页面",
        "priority": "high",
        "estimated_hours": 4,
    },
    {
        "name": "忘记密码页面",
        "description": "实现密码重置请求界面",
        "phase": "Phase 6",
        "milestone": "Week 3: 认证页面",
        "priority": "high",
        "estimated_hours": 3,
    },
    {
        "name": "重置密码页面",
        "description": "实现密码重置界面",
        "phase": "Phase 6",
        "milestone": "Week 3: 认证页面",
        "priority": "high",
        "estimated_hours": 3,
    },
    {
        "name": "Token 管理和刷新",
        "description": "实现自动 token 刷新和过期处理",
        "phase": "Phase 6",
        "milestone": "Week 3: 认证页面",
        "priority": "high",
        "estimated_hours": 4,
    },
    # Week 4: 工作空间管理 (6个任务)
    {
        "name": "工作空间列表页面",
        "description": "显示所有工作空间，支持创建",
        "phase": "Phase 6",
        "milestone": "Week 4: 工作空间管理",
        "priority": "high",
        "estimated_hours": 5,
    },
    {
        "name": "工作空间详情页面",
        "description": "显示工作空间概览、配额使用情况",
        "phase": "Phase 6",
        "milestone": "Week 4: 工作空间管理",
        "priority": "high",
        "estimated_hours": 4,
    },
    {
        "name": "成员列表和管理",
        "description": "显示成员列表，支持角色修改和移除",
        "phase": "Phase 6",
        "milestone": "Week 4: 工作空间管理",
        "priority": "high",
        "estimated_hours": 5,
    },
    {
        "name": "邀请成员功能",
        "description": "实现邀请成员弹窗和邮件发送",
        "phase": "Phase 6",
        "milestone": "Week 4: 工作空间管理",
        "priority": "high",
        "estimated_hours": 4,
    },
    {
        "name": "权限矩阵展示",
        "description": "显示各角色的权限说明",
        "phase": "Phase 6",
        "milestone": "Week 4: 工作空间管理",
        "priority": "medium",
        "estimated_hours": 2,
    },
    {
        "name": "工作空间设置",
        "description": "修改名称、删除工作空间等",
        "phase": "Phase 6",
        "milestone": "Week 4: 工作空间管理",
        "priority": "medium",
        "estimated_hours": 3,
    },
    # Week 5: 订阅管理 (5个任务)
    {
        "name": "套餐选择页面",
        "description": "显示 Free/Pro/Enterprise 套餐对比",
        "phase": "Phase 6",
        "milestone": "Week 5: 订阅管理",
        "priority": "high",
        "estimated_hours": 5,
    },
    {
        "name": "升级流程",
        "description": "实现订阅升级流程和确认",
        "phase": "Phase 6",
        "milestone": "Week 5: 订阅管理",
        "priority": "high",
        "estimated_hours": 4,
    },
    {
        "name": "配额展示组件",
        "description": "显示项目数量、存储使用情况",
        "phase": "Phase 6",
        "milestone": "Week 5: 订阅管理",
        "priority": "high",
        "estimated_hours": 3,
    },
    {
        "name": "账单页面",
        "description": "显示历史账单列表",
        "phase": "Phase 6",
        "milestone": "Week 5: 订阅管理",
        "priority": "medium",
        "estimated_hours": 4,
    },
    {
        "name": "发票申请入口",
        "description": "提供发票申请表单",
        "phase": "Phase 6",
        "milestone": "Week 5: 订阅管理",
        "priority": "low",
        "estimated_hours": 3,
    },
    # Week 6: Admin 后台 (5个任务)
    {
        "name": "Dashboard 仪表盘",
        "description": "显示关键指标和图表",
        "phase": "Phase 6",
        "milestone": "Week 6: Admin 后台",
        "priority": "high",
        "estimated_hours": 6,
    },
    {
        "name": "用户管理页面",
        "description": "用户列表、搜索、详情",
        "phase": "Phase 6",
        "milestone": "Week 6: Admin 后台",
        "priority": "high",
        "estimated_hours": 5,
    },
    {
        "name": "用户操作功能",
        "description": "封禁、解封、重置密码等",
        "phase": "Phase 6",
        "milestone": "Week 6: Admin 后台",
        "priority": "high",
        "estimated_hours": 4,
    },
    {
        "name": "系统监控页面",
        "description": "显示 API 性能、数据库状态",
        "phase": "Phase 6",
        "milestone": "Week 6: Admin 后台",
        "priority": "medium",
        "estimated_hours": 5,
    },
    {
        "name": "日志查看器",
        "description": "查看错误日志和慢查询",
        "phase": "Phase 6",
        "milestone": "Week 6: Admin 后台",
        "priority": "medium",
        "estimated_hours": 4,
    },
    # Week 7: 个人中心 (4个任务)
    {
        "name": "个人设置页面",
        "description": "修改昵称、头像等",
        "phase": "Phase 6",
        "milestone": "Week 7: 个人中心",
        "priority": "medium",
        "estimated_hours": 4,
    },
    {
        "name": "账号安全设置",
        "description": "修改密码、邮箱等",
        "phase": "Phase 6",
        "milestone": "Week 7: 个人中心",
        "priority": "high",
        "estimated_hours": 4,
    },
    {
        "name": "通知设置",
        "description": "配置邮件通知偏好",
        "phase": "Phase 6",
        "milestone": "Week 7: 个人中心",
        "priority": "low",
        "estimated_hours": 3,
    },
    {
        "name": "Session 管理",
        "description": "查看活跃设备、强制登出",
        "phase": "Phase 6",
        "milestone": "Week 7: 个人中心",
        "priority": "medium",
        "estimated_hours": 4,
    },
    # Week 8: 测试和优化 (8个任务)
    {
        "name": "E2E 测试：认证流程",
        "description": "使用 Playwright 测试注册登录流程",
        "phase": "Phase 6",
        "milestone": "Week 8: 测试和优化",
        "priority": "high",
        "estimated_hours": 4,
    },
    {
        "name": "E2E 测试：工作空间",
        "description": "测试工作空间创建和管理",
        "phase": "Phase 6",
        "milestone": "Week 8: 测试和优化",
        "priority": "high",
        "estimated_hours": 4,
    },
    {
        "name": "E2E 测试：订阅升级",
        "description": "测试订阅升级流程",
        "phase": "Phase 6",
        "milestone": "Week 8: 测试和优化",
        "priority": "high",
        "estimated_hours": 3,
    },
    {
        "name": "性能优化：代码分割",
        "description": "实现路由懒加载和代码分割",
        "phase": "Phase 6",
        "milestone": "Week 8: 测试和优化",
        "priority": "high",
        "estimated_hours": 3,
    },
    {
        "name": "性能优化：图片和资源",
        "description": "优化图片加载和资源缓存",
        "phase": "Phase 6",
        "milestone": "Week 8: 测试和优化",
        "priority": "medium",
        "estimated_hours": 3,
    },
    {
        "name": "可访问性优化",
        "description": "添加 ARIA 标签，键盘导航支持",
        "phase": "Phase 6",
        "milestone": "Week 8: 测试和优化",
        "priority": "medium",
        "estimated_hours": 4,
    },
    {
        "name": "移动端适配优化",
        "description": "优化移动端体验和响应式布局",
        "phase": "Phase 6",
        "milestone": "Week 8: 测试和优化",
        "priority": "medium",
        "estimated_hours": 5,
    },
    {
        "name": "Bug 修复和收尾",
        "description": "修复测试发现的 Bug，完善细节",
        "phase": "Phase 6",
        "milestone": "Week 8: 测试和优化",
        "priority": "high",
        "estimated_hours": 6,
    },
]


def init_phase6_tasks():
    """初始化 Phase 6 任务到数据库"""

    # 连接数据库
    db_path = "F:/openclaw-saas/tracker.db"
    conn = sqlite3.connect(db_path)
    cursor = conn.cursor()

    print("[Phase 6] 开始初始化任务...")

    # 插入任务
    created_count = 0
    for task in PHASE6_TASKS:
        try:
            cursor.execute(
                """
                INSERT INTO tasks (
                    name, description, status, phase, milestone,
                    priority, estimated_hours, created_at
                ) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
            """,
                (
                    task["name"],
                    task["description"],
                    "pending",
                    task["phase"],
                    task["milestone"],
                    task["priority"],
                    task["estimated_hours"],
                    datetime.now().isoformat(),
                ),
            )
            created_count += 1
            print(f"[OK] {task['name']}")
        except Exception as e:
            print(f"[ERROR] {task['name']}: {e}")

    conn.commit()
    conn.close()

    print(f"\n[SUCCESS] Phase 6 任务初始化完成！")
    print(f"📊 总计 {len(PHASE6_TASKS)} 个任务")
    print(f"✅ 成功创建 {created_count} 个任务")
    print(f"\n任务分布：")
    print(f"  Week 1-2: 基础搭建 - 7 个任务")
    print(f"  Week 3: 认证页面 - 5 个任务")
    print(f"  Week 4: 工作空间管理 - 6 个任务")
    print(f"  Week 5: 订阅管理 - 5 个任务")
    print(f"  Week 6: Admin 后台 - 5 个任务")
    print(f"  Week 7: 个人中心 - 4 个任务")
    print(f"  Week 8: 测试和优化 - 8 个任务")
    print(f"\n预计总工时：{sum(t['estimated_hours'] for t in PHASE6_TASKS)} 小时")


if __name__ == "__main__":
    init_phase6_tasks()
