-- ========================================
-- 005-validate-all.sql
-- ========================================
-- 目的：全局验证10个Schema和数据迁移的完整性
-- 预计时间：10分钟
-- 作者：AI助手
-- 日期：2025-11-09
-- ========================================

-- 前置条件：
-- 1. 已执行 001-create-all-10-schemas.sql
-- 2. 已执行 002-migrate-platform.sql
-- 3. 已执行 003-migrate-aia.sql
-- 4. 已执行 004-migrate-pkb.sql

-- ========================================
-- 第一部分：Schema完整性验证
-- ========================================

\echo '========================================';
\echo '第一部分：Schema完整性验证';
\echo '========================================';

SELECT 
    'Schema检查' AS check_type,
    nspname AS schema_name,
    CASE 
        WHEN nspname IN ('platform_schema', 'aia_schema', 'pkb_schema', 
                         'asl_schema', 'common_schema', 'dc_schema',
                         'rvw_schema', 'admin_schema', 'ssa_schema', 'st_schema')
        THEN '✅ 存在'
        ELSE '❌ 不存在'
    END AS status,
    pg_catalog.obj_description(oid, 'pg_namespace') AS description
FROM pg_namespace
WHERE nspname IN (
    'platform_schema', 'aia_schema', 'pkb_schema',
    'asl_schema', 'common_schema', 'dc_schema',
    'rvw_schema', 'admin_schema', 'ssa_schema', 'st_schema'
)
ORDER BY nspname;

-- 统计Schema数量
DO $$
DECLARE
    schema_count INTEGER;
BEGIN
    SELECT COUNT(*) INTO schema_count
    FROM pg_namespace
    WHERE nspname 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 '✅ Schema完整性检查通过：10个Schema全部创建成功';
    ELSE
        RAISE WARNING '❌ Schema完整性检查失败：预期10个，实际%个', schema_count;
    END IF;
    RAISE NOTICE '========================================';
END $$;

-- ========================================
-- 第二部分：表结构验证
-- ========================================

\echo '';
\echo '========================================';
\echo '第二部分：表结构验证';
\echo '========================================';

SELECT 
    'platform_schema' AS schema_name,
    tablename AS table_name,
    '✅' AS status
FROM pg_tables
WHERE schemaname = 'platform_schema'

UNION ALL

SELECT 
    'aia_schema',
    tablename,
    '✅'
FROM pg_tables
WHERE schemaname = 'aia_schema'

UNION ALL

SELECT 
    'pkb_schema',
    tablename,
    '✅'
FROM pg_tables
WHERE schemaname = 'pkb_schema'

ORDER BY schema_name, table_name;

-- 统计各Schema的表数量
DO $$
DECLARE
    platform_tables INTEGER;
    aia_tables INTEGER;
    pkb_tables INTEGER;
    all_ok BOOLEAN := true;
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;
    RAISE NOTICE '========================================';
    
    IF platform_tables != 1 THEN
        RAISE WARNING '❌ platform_schema表数量异常';
        all_ok := false;
    END IF;
    
    IF aia_tables != 5 THEN
        RAISE WARNING '❌ aia_schema表数量异常';
        all_ok := false;
    END IF;
    
    IF pkb_tables != 5 THEN
        RAISE WARNING '❌ pkb_schema表数量异常';
        all_ok := false;
    END IF;
    
    IF all_ok THEN
        RAISE NOTICE '✅ 表结构验证通过：所有表创建成功';
    END IF;
    RAISE NOTICE '========================================';
END $$;

-- ========================================
-- 第三部分：数据量对比验证
-- ========================================

\echo '';
\echo '========================================';
\echo '第三部分：数据量对比验证';
\echo '========================================';

-- 3.1 Platform Schema
SELECT 
    'users' AS table_name,
    (SELECT COUNT(*) FROM public.users) AS public_count,
    (SELECT COUNT(*) FROM platform_schema.users) AS migrated_count,
    CASE 
        WHEN (SELECT COUNT(*) FROM public.users) = (SELECT COUNT(*) FROM platform_schema.users)
        THEN '✅ 一致'
        ELSE '❌ 不一致'
    END AS status
WHERE EXISTS (SELECT 1 FROM public.users LIMIT 1);

-- 3.2 AIA Schema
SELECT 
    'projects' AS table_name,
    (SELECT COUNT(*) FROM public.projects) AS public_count,
    (SELECT COUNT(*) FROM aia_schema.projects) AS migrated_count,
    CASE 
        WHEN (SELECT COUNT(*) FROM public.projects) = (SELECT COUNT(*) FROM aia_schema.projects)
        THEN '✅ 一致'
        ELSE '❌ 不一致'
    END AS status

UNION ALL

SELECT 
    'conversations',
    (SELECT COUNT(*) FROM public.conversations),
    (SELECT COUNT(*) FROM aia_schema.conversations),
    CASE 
        WHEN (SELECT COUNT(*) FROM public.conversations) = (SELECT COUNT(*) FROM aia_schema.conversations)
        THEN '✅ 一致'
        ELSE '❌ 不一致'
    END

UNION ALL

SELECT 
    'messages',
    (SELECT COUNT(*) FROM public.messages),
    (SELECT COUNT(*) FROM aia_schema.messages),
    CASE 
        WHEN (SELECT COUNT(*) FROM public.messages) = (SELECT COUNT(*) FROM aia_schema.messages)
        THEN '✅ 一致'
        ELSE '❌ 不一致'
    END

UNION ALL

SELECT 
    'general_conversations',
    (SELECT COUNT(*) FROM public.general_conversations),
    (SELECT COUNT(*) FROM aia_schema.general_conversations),
    CASE 
        WHEN (SELECT COUNT(*) FROM public.general_conversations) = (SELECT COUNT(*) FROM aia_schema.general_conversations)
        THEN '✅ 一致'
        ELSE '❌ 不一致'
    END

UNION ALL

SELECT 
    'general_messages',
    (SELECT COUNT(*) FROM public.general_messages),
    (SELECT COUNT(*) FROM aia_schema.general_messages),
    CASE 
        WHEN (SELECT COUNT(*) FROM public.general_messages) = (SELECT COUNT(*) FROM aia_schema.general_messages)
        THEN '✅ 一致'
        ELSE '❌ 不一致'
    END;

-- 3.3 PKB Schema
SELECT 
    'knowledge_bases' AS table_name,
    (SELECT COUNT(*) FROM public.knowledge_bases) AS public_count,
    (SELECT COUNT(*) FROM pkb_schema.knowledge_bases) AS migrated_count,
    CASE 
        WHEN (SELECT COUNT(*) FROM public.knowledge_bases) = (SELECT COUNT(*) FROM pkb_schema.knowledge_bases)
        THEN '✅ 一致'
        ELSE '❌ 不一致'
    END AS status

UNION ALL

SELECT 
    'documents',
    (SELECT COUNT(*) FROM public.documents),
    (SELECT COUNT(*) FROM pkb_schema.documents),
    CASE 
        WHEN (SELECT COUNT(*) FROM public.documents) = (SELECT COUNT(*) FROM pkb_schema.documents)
        THEN '✅ 一致'
        ELSE '❌ 不一致'
    END

UNION ALL

SELECT 
    'batch_tasks',
    (SELECT COUNT(*) FROM public.batch_tasks),
    (SELECT COUNT(*) FROM pkb_schema.batch_tasks),
    CASE 
        WHEN (SELECT COUNT(*) FROM public.batch_tasks) = (SELECT COUNT(*) FROM pkb_schema.batch_tasks)
        THEN '✅ 一致'
        ELSE '❌ 不一致'
    END

UNION ALL

SELECT 
    'batch_results',
    (SELECT COUNT(*) FROM public.batch_results),
    (SELECT COUNT(*) FROM pkb_schema.batch_results),
    CASE 
        WHEN (SELECT COUNT(*) FROM public.batch_results) = (SELECT COUNT(*) FROM pkb_schema.batch_results)
        THEN '✅ 一致'
        ELSE '❌ 不一致'
    END

UNION ALL

SELECT 
    'task_templates',
    (SELECT COUNT(*) FROM public.task_templates),
    (SELECT COUNT(*) FROM pkb_schema.task_templates),
    CASE 
        WHEN (SELECT COUNT(*) FROM public.task_templates) = (SELECT COUNT(*) FROM pkb_schema.task_templates)
        THEN '✅ 一致'
        ELSE '❌ 不一致'
    END;

-- ========================================
-- 第四部分：外键约束验证
-- ========================================

\echo '';
\echo '========================================';
\echo '第四部分：外键约束验证';
\echo '========================================';

SELECT 
    tc.table_schema AS schema_name,
    tc.table_name,
    tc.constraint_name,
    kcu.column_name,
    ccu.table_schema AS foreign_schema,
    ccu.table_name AS foreign_table,
    ccu.column_name AS foreign_column,
    '✅' AS status
FROM information_schema.table_constraints tc
JOIN information_schema.key_column_usage kcu
    ON tc.constraint_name = kcu.constraint_name
    AND tc.table_schema = kcu.table_schema
JOIN information_schema.constraint_column_usage ccu
    ON ccu.constraint_name = tc.constraint_name
    AND ccu.table_schema = tc.table_schema
WHERE tc.constraint_type = 'FOREIGN KEY'
    AND tc.table_schema IN ('platform_schema', 'aia_schema', 'pkb_schema')
ORDER BY tc.table_schema, tc.table_name, tc.constraint_name;

-- 统计外键数量
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 '========================================';
END $$;

-- ========================================
-- 第五部分：跨Schema引用验证
-- ========================================

\echo '';
\echo '========================================';
\echo '第五部分：跨Schema引用验证';
\echo '========================================';

-- 验证所有引用platform_schema.users的外键是否有效
DO $$
DECLARE
    invalid_aia_projects INTEGER;
    invalid_aia_conversations INTEGER;
    invalid_aia_general_conversations INTEGER;
    invalid_pkb_knowledge_bases INTEGER;
    invalid_pkb_documents INTEGER;
    all_valid BOOLEAN := true;
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;
    
    -- 验证aia_schema.conversations
    SELECT COUNT(*) INTO invalid_aia_conversations
    FROM aia_schema.conversations c
    LEFT JOIN platform_schema.users u ON c.user_id = u.id
    WHERE u.id IS NULL;
    
    -- 验证aia_schema.general_conversations
    SELECT COUNT(*) INTO invalid_aia_general_conversations
    FROM aia_schema.general_conversations gc
    LEFT JOIN platform_schema.users u ON gc.user_id = u.id
    WHERE u.id IS NULL;
    
    -- 验证pkb_schema.knowledge_bases
    SELECT COUNT(*) INTO invalid_pkb_knowledge_bases
    FROM pkb_schema.knowledge_bases kb
    LEFT JOIN platform_schema.users u ON kb.user_id = u.id
    WHERE u.id IS NULL;
    
    -- 验证pkb_schema.documents
    SELECT COUNT(*) INTO invalid_pkb_documents
    FROM pkb_schema.documents d
    LEFT JOIN platform_schema.users u ON d.user_id = u.id
    WHERE u.id IS NULL;
    
    RAISE NOTICE '========================================';
    RAISE NOTICE '跨Schema引用验证结果：';
    RAISE NOTICE '----------------------------------------';
    
    IF invalid_aia_projects > 0 THEN
        RAISE WARNING '❌ aia_schema.projects有%条无效user_id', invalid_aia_projects;
        all_valid := false;
    ELSE
        RAISE NOTICE '✅ aia_schema.projects外键全部有效';
    END IF;
    
    IF invalid_aia_conversations > 0 THEN
        RAISE WARNING '❌ aia_schema.conversations有%条无效user_id', invalid_aia_conversations;
        all_valid := false;
    ELSE
        RAISE NOTICE '✅ aia_schema.conversations外键全部有效';
    END IF;
    
    IF invalid_aia_general_conversations > 0 THEN
        RAISE WARNING '❌ aia_schema.general_conversations有%条无效user_id', invalid_aia_general_conversations;
        all_valid := false;
    ELSE
        RAISE NOTICE '✅ aia_schema.general_conversations外键全部有效';
    END IF;
    
    IF invalid_pkb_knowledge_bases > 0 THEN
        RAISE WARNING '❌ pkb_schema.knowledge_bases有%条无效user_id', invalid_pkb_knowledge_bases;
        all_valid := false;
    ELSE
        RAISE NOTICE '✅ pkb_schema.knowledge_bases外键全部有效';
    END IF;
    
    IF invalid_pkb_documents > 0 THEN
        RAISE WARNING '❌ pkb_schema.documents有%条无效user_id', invalid_pkb_documents;
        all_valid := false;
    ELSE
        RAISE NOTICE '✅ pkb_schema.documents外键全部有效';
    END IF;
    
    RAISE NOTICE '========================================';
    
    IF all_valid THEN
        RAISE NOTICE '✅ 所有跨Schema引用验证通过';
    ELSE
        RAISE WARNING '❌ 存在无效的跨Schema引用';
    END IF;
    
    RAISE NOTICE '========================================';
END $$;

-- ========================================
-- 第六部分：索引验证
-- ========================================

\echo '';
\echo '========================================';
\echo '第六部分：索引验证';
\echo '========================================';

SELECT 
    schemaname AS schema_name,
    tablename AS table_name,
    indexname AS index_name,
    '✅' AS status
FROM pg_indexes
WHERE schemaname IN ('platform_schema', 'aia_schema', 'pkb_schema')
ORDER BY schemaname, tablename, indexname;

-- 统计索引数量
DO $$
DECLARE
    index_count INTEGER;
BEGIN
    SELECT COUNT(*) INTO index_count
    FROM pg_indexes
    WHERE schemaname IN ('platform_schema', 'aia_schema', 'pkb_schema')
        AND indexname NOT LIKE '%pkey'; -- 排除主键索引
    
    RAISE NOTICE '========================================';
    RAISE NOTICE '✅ 非主键索引总数：%', index_count;
    RAISE NOTICE '========================================';
END $$;

-- ========================================
-- 第七部分：数据采样验证（抽样检查）
-- ========================================

\echo '';
\echo '========================================';
\echo '第七部分：数据采样验证';
\echo '========================================';

-- 采样验证：检查ID是否完全一致
DO $$
DECLARE
    user_ids_match BOOLEAN;
    project_ids_match BOOLEAN;
BEGIN
    -- 验证users的ID
    SELECT NOT EXISTS (
        SELECT id FROM public.users
        EXCEPT
        SELECT id FROM platform_schema.users
    ) AND NOT EXISTS (
        SELECT id FROM platform_schema.users
        EXCEPT
        SELECT id FROM public.users
    ) INTO user_ids_match;
    
    -- 验证projects的ID
    SELECT NOT EXISTS (
        SELECT id FROM public.projects
        EXCEPT
        SELECT id FROM aia_schema.projects
    ) AND NOT EXISTS (
        SELECT id FROM aia_schema.projects
        EXCEPT
        SELECT id FROM public.projects
    ) INTO project_ids_match;
    
    RAISE NOTICE '========================================';
    IF user_ids_match THEN
        RAISE NOTICE '✅ users表ID完全一致';
    ELSE
        RAISE WARNING '❌ users表ID存在差异';
    END IF;
    
    IF project_ids_match THEN
        RAISE NOTICE '✅ projects表ID完全一致';
    ELSE
        RAISE WARNING '❌ projects表ID存在差异';
    END IF;
    RAISE NOTICE '========================================';
END $$;

-- ========================================
-- 第八部分：最终总结报告
-- ========================================

\echo '';
\echo '========================================';
\echo '最终总结报告';
\echo '========================================';

SELECT 
    '✅ Schema隔离迁移验证完成' AS summary,
    (SELECT COUNT(*) FROM platform_schema.users) || ' users' AS platform,
    (SELECT COUNT(*) FROM aia_schema.projects) || ' projects, ' ||
    (SELECT COUNT(*) FROM aia_schema.conversations) || ' conversations' AS aia,
    (SELECT COUNT(*) FROM pkb_schema.knowledge_bases) || ' knowledge_bases, ' ||
    (SELECT COUNT(*) FROM pkb_schema.documents) || ' documents' AS pkb;

\echo '';
\echo '========================================';
\echo '✅ 所有验证完成！';
\echo '========================================';
\echo '下一步：';
\echo '1. 更新Prisma配置（schema.prisma）';
\echo '2. 生成Prisma Client';
\echo '3. 更新代码以使用新Schema';
\echo '4. 运行应用测试';
\echo '========================================';

-- ========================================
-- 完成
-- ========================================



















