-- ========================================
-- 002-migrate-platform.sql
-- ========================================
-- 目的：迁移platform_schema（用户表）
-- 迁移表：1个（users）
-- 预计时间：15分钟
-- 作者：AI助手
-- 日期：2025-11-09
-- ========================================

-- 前置条件：
-- 1. 已执行 001-create-all-10-schemas.sql
-- 2. public.users 表存在且有数据

BEGIN;

-- ========================================
-- 第一步：创建platform_schema.users表
-- ========================================

CREATE TABLE IF NOT EXISTS platform_schema.users (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    email VARCHAR(255) UNIQUE NOT NULL,
    password VARCHAR(255) NOT NULL,
    name VARCHAR(255),
    avatar_url VARCHAR(500),
    role VARCHAR(50) NOT NULL DEFAULT 'user',
    status VARCHAR(50) DEFAULT 'active',
    kb_quota INT DEFAULT 3,
    kb_used INT DEFAULT 0,
    trial_ends_at TIMESTAMP,
    is_trial BOOLEAN DEFAULT true,
    last_login_at TIMESTAMP,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- ========================================
-- 第二步：创建索引
-- ========================================

CREATE INDEX IF NOT EXISTS idx_platform_users_email ON platform_schema.users(email);
CREATE INDEX IF NOT EXISTS idx_platform_users_role ON platform_schema.users(role);
CREATE INDEX IF NOT EXISTS idx_platform_users_status ON platform_schema.users(status);
CREATE INDEX IF NOT EXISTS idx_platform_users_created_at ON platform_schema.users(created_at);

-- ========================================
-- 第三步：迁移数据
-- ========================================

-- 从public.users迁移数据到platform_schema.users
INSERT INTO platform_schema.users (
    id, email, password, name, avatar_url,
    role, status, kb_quota, kb_used,
    trial_ends_at, is_trial, last_login_at,
    created_at, updated_at
)
SELECT 
    id, email, password, name, avatar_url,
    role, status, kb_quota, kb_used,
    trial_ends_at, is_trial, last_login_at,
    created_at, updated_at
FROM public.users
ON CONFLICT (id) DO NOTHING;  -- 如果已存在则跳过（支持重复执行）

-- ========================================
-- 第四步：数据验证
-- ========================================

DO $$
DECLARE
    public_count INTEGER;
    platform_count INTEGER;
BEGIN
    -- 统计原表数据量
    SELECT COUNT(*) INTO public_count FROM public.users;
    
    -- 统计新表数据量
    SELECT COUNT(*) INTO platform_count FROM platform_schema.users;
    
    RAISE NOTICE '原表 public.users 数据量：%', public_count;
    RAISE NOTICE '新表 platform_schema.users 数据量：%', platform_count;
    
    -- 验证数据一致性
    IF public_count = platform_count THEN
        RAISE NOTICE '✅ 数据迁移成功：数据量完全一致';
    ELSE
        RAISE WARNING '⚠️ 警告：数据量不一致！预期 %，实际 %', public_count, platform_count;
    END IF;
    
    -- 验证email唯一性
    IF (SELECT COUNT(DISTINCT email) FROM platform_schema.users) = platform_count THEN
        RAISE NOTICE '✅ Email唯一性校验通过';
    ELSE
        RAISE WARNING '⚠️ 警告：Email存在重复';
    END IF;
END $$;

-- ========================================
-- 第五步：对比验证（抽样检查）
-- ========================================

-- 对比前5条数据
SELECT 'public.users' AS source, id, email, name, role, created_at
FROM public.users
ORDER BY created_at DESC
LIMIT 5;

SELECT 'platform_schema.users' AS source, id, email, name, role, created_at
FROM platform_schema.users
ORDER BY created_at DESC
LIMIT 5;

COMMIT;

-- ========================================
-- 执行结果统计（可单独运行）
-- ========================================

SELECT 
    'platform_schema.users' AS table_name,
    COUNT(*) AS total_count,
    COUNT(DISTINCT email) AS unique_emails,
    COUNT(CASE WHEN role = 'admin' THEN 1 END) AS admin_count,
    COUNT(CASE WHEN role = 'user' THEN 1 END) AS user_count,
    COUNT(CASE WHEN status = 'active' THEN 1 END) AS active_count,
    MIN(created_at) AS first_user_date,
    MAX(created_at) AS last_user_date
FROM platform_schema.users;

-- ========================================
-- 后续步骤说明
-- ========================================
-- 注意：public.users表暂时保留，不删除
-- 原因：
-- 1. 其他Schema的表（aia, pkb）会引用platform_schema.users
-- 2. 所有迁移完成并验证后，再决定是否删除public.users
-- 3. 删除前需确保所有外键已更新
-- ========================================



















