-- ========================================
-- 简化版验证脚本
-- ========================================

-- 验证10个Schema是否创建
DO $$
DECLARE
    schema_count INTEGER;
BEGIN
    SELECT COUNT(*) INTO schema_count
    FROM information_schema.schemata
    WHERE schema_name IN (
        'platform_schema', 'aia_schema', 'pkb_schema',
        'asl_schema', 'common_schema', 'dc_schema',
        'rvw_schema', 'admin_schema', 'ssa_schema', 'st_schema'
    );
    
    RAISE NOTICE '========================================';
    IF schema_count = 10 THEN
        RAISE NOTICE '✅ 10个Schema全部创建成功';
    ELSE
        RAISE WARNING '⚠️ Schema数量异常: 预期10个，实际%个', schema_count;
    END IF;
    RAISE NOTICE '========================================';
END $$;

-- 验证表数量
DO $$
DECLARE
    platform_tables INTEGER;
    aia_tables INTEGER;
    pkb_tables INTEGER;
BEGIN
    SELECT COUNT(*) INTO platform_tables FROM pg_tables WHERE schemaname = 'platform_schema';
    SELECT COUNT(*) INTO aia_tables FROM pg_tables WHERE schemaname = 'aia_schema';
    SELECT COUNT(*) INTO pkb_tables FROM pg_tables WHERE schemaname = 'pkb_schema';
    
    RAISE NOTICE '表数量统计：';
    RAISE NOTICE 'platform_schema: % 个表 (预期1个)', platform_tables;
    RAISE NOTICE 'aia_schema: % 个表 (预期5个)', aia_tables;
    RAISE NOTICE 'pkb_schema: % 个表 (预期5个)', pkb_tables;
    
    IF platform_tables = 1 AND aia_tables = 5 AND pkb_tables = 5 THEN
        RAISE NOTICE '✅ 所有表创建成功';
    ELSE
        RAISE WARNING '⚠️ 表数量存在异常';
    END IF;
    RAISE NOTICE '========================================';
END $$;

-- 验证数据量
DO $$
DECLARE
    public_users INTEGER;
    platform_users INTEGER;
    public_projects INTEGER;
    aia_projects INTEGER;
    public_kb INTEGER;
    pkb_kb INTEGER;
BEGIN
    -- 统计users
    SELECT COUNT(*) INTO public_users FROM public.users;
    SELECT COUNT(*) INTO platform_users FROM platform_schema.users;
    
    -- 统计projects
    SELECT COUNT(*) INTO public_projects FROM public.projects;
    SELECT COUNT(*) INTO aia_projects FROM aia_schema.projects;
    
    -- 统计knowledge_bases
    SELECT COUNT(*) INTO public_kb FROM public.knowledge_bases;
    SELECT COUNT(*) INTO pkb_kb FROM pkb_schema.knowledge_bases;
    
    RAISE NOTICE '数据量对比：';
    RAISE NOTICE 'users: public.% -> platform_schema.%', public_users, platform_users;
    RAISE NOTICE 'projects: public.% -> aia_schema.%', public_projects, aia_projects;
    RAISE NOTICE 'knowledge_bases: public.% -> pkb_schema.%', public_kb, pkb_kb;
    
    IF public_users = platform_users AND 
       public_projects = aia_projects AND 
       public_kb = pkb_kb THEN
        RAISE NOTICE '✅ 数据迁移完整';
    ELSE
        RAISE WARNING '⚠️ 数据量存在差异';
    END IF;
    RAISE NOTICE '========================================';
END $$;

-- 验证外键约束
DO $$
DECLARE
    fk_count INTEGER;
BEGIN
    SELECT COUNT(*) INTO fk_count
    FROM information_schema.table_constraints
    WHERE constraint_type = 'FOREIGN KEY'
        AND table_schema IN ('platform_schema', 'aia_schema', 'pkb_schema');
    
    RAISE NOTICE '外键约束统计：';
    RAISE NOTICE '外键约束总数：%', fk_count;
    RAISE NOTICE '✅ 外键约束已建立';
    RAISE NOTICE '========================================';
END $$;

-- 验证跨Schema引用
DO $$
DECLARE
    invalid_aia_projects INTEGER;
    invalid_pkb_kb INTEGER;
BEGIN
    -- 验证aia_schema.projects
    SELECT COUNT(*) INTO invalid_aia_projects
    FROM aia_schema.projects p
    LEFT JOIN platform_schema.users u ON p.user_id = u.id
    WHERE u.id IS NULL;
    
    -- 验证pkb_schema.knowledge_bases
    SELECT COUNT(*) INTO invalid_pkb_kb
    FROM pkb_schema.knowledge_bases kb
    LEFT JOIN platform_schema.users u ON kb.user_id = u.id
    WHERE u.id IS NULL;
    
    RAISE NOTICE '跨Schema引用验证：';
    IF invalid_aia_projects = 0 THEN
        RAISE NOTICE '✅ aia_schema.projects外键全部有效';
    ELSE
        RAISE WARNING '⚠️ aia_schema.projects有%条无效user_id', invalid_aia_projects;
    END IF;
    
    IF invalid_pkb_kb = 0 THEN
        RAISE NOTICE '✅ pkb_schema.knowledge_bases外键全部有效';
    ELSE
        RAISE WARNING '⚠️ pkb_schema.knowledge_bases有%条无效user_id', invalid_pkb_kb;
    END IF;
    RAISE NOTICE '========================================';
END $$;

-- 最终总结
DO $$
BEGIN
    RAISE NOTICE '';
    RAISE NOTICE '🎉🎉🎉 Schema迁移验证完成 🎉🎉🎉';
    RAISE NOTICE '';
    RAISE NOTICE '下一步：';
    RAISE NOTICE '1. 更新Prisma配置（schema.prisma）';
    RAISE NOTICE '2. 生成Prisma Client';
    RAISE NOTICE '3. 更新代码以使用新Schema';
    RAISE NOTICE '4. 测试现有功能';
    RAISE NOTICE '';
    RAISE NOTICE '========================================';
END $$;





