-- ========================================
-- 数据库迁移验证脚本（纯SQL版本）
-- ========================================

-- 1. Schema检查
SELECT '=== 1. Schema检查 ===' as section;
SELECT 
    nspname as schema_name,
    (SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = s.nspname) as table_count
FROM pg_namespace s
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;

-- 2. 表清单
SELECT '=== 2. Platform Schema 表清单 ===' as section;
SELECT tablename FROM pg_tables WHERE schemaname = 'platform_schema' ORDER BY tablename;

SELECT '=== 3. AIA Schema 表清单 ===' as section;
SELECT tablename FROM pg_tables WHERE schemaname = 'aia_schema' ORDER BY tablename;

SELECT '=== 4. PKB Schema 表清单 ===' as section;
SELECT tablename FROM pg_tables WHERE schemaname = 'pkb_schema' ORDER BY tablename;

-- 3. 数据量统计
SELECT '=== 5. 数据量统计 ===' as section;
SELECT 'platform_schema.users' AS table_name, COUNT(*) AS row_count FROM platform_schema.users
UNION ALL
SELECT 'aia_schema.projects', COUNT(*) FROM aia_schema.projects
UNION ALL
SELECT 'aia_schema.conversations', COUNT(*) FROM aia_schema.conversations
UNION ALL
SELECT 'aia_schema.messages', COUNT(*) FROM aia_schema.messages
UNION ALL
SELECT 'aia_schema.general_conversations', COUNT(*) FROM aia_schema.general_conversations
UNION ALL
SELECT 'aia_schema.general_messages', COUNT(*) FROM aia_schema.general_messages
UNION ALL
SELECT 'pkb_schema.knowledge_bases', COUNT(*) FROM pkb_schema.knowledge_bases
UNION ALL
SELECT 'pkb_schema.documents', COUNT(*) FROM pkb_schema.documents
UNION ALL
SELECT 'pkb_schema.batch_tasks', COUNT(*) FROM pkb_schema.batch_tasks
UNION ALL
SELECT 'pkb_schema.batch_results', COUNT(*) FROM pkb_schema.batch_results
UNION ALL
SELECT 'pkb_schema.task_templates', COUNT(*) FROM pkb_schema.task_templates;

-- 4. 数据完整性对比
SELECT '=== 6. 数据完整性对比（public vs 新Schema） ===' as section;
SELECT 
    'users' AS table_name,
    (SELECT COUNT(*) FROM public.users) AS public_count,
    (SELECT COUNT(*) FROM platform_schema.users) AS new_count,
    CASE 
        WHEN (SELECT COUNT(*) FROM public.users) = (SELECT COUNT(*) FROM platform_schema.users)
        THEN 'OK'
        ELSE 'MISMATCH'
    END AS status
UNION ALL
SELECT 'projects',
    (SELECT COUNT(*) FROM public.projects),
    (SELECT COUNT(*) FROM aia_schema.projects),
    CASE WHEN (SELECT COUNT(*) FROM public.projects) = (SELECT COUNT(*) FROM aia_schema.projects) THEN 'OK' ELSE 'MISMATCH' END
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 'OK' ELSE 'MISMATCH' 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 'OK' ELSE 'MISMATCH' END
UNION ALL
SELECT 'knowledge_bases',
    (SELECT COUNT(*) FROM public.knowledge_bases),
    (SELECT COUNT(*) FROM pkb_schema.knowledge_bases),
    CASE WHEN (SELECT COUNT(*) FROM public.knowledge_bases) = (SELECT COUNT(*) FROM pkb_schema.knowledge_bases) THEN 'OK' ELSE 'MISMATCH' END
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 'OK' ELSE 'MISMATCH' END;

-- 5. 外键约束统计
SELECT '=== 7. 外键约束统计 ===' as section;
SELECT 
    table_schema,
    COUNT(*) as fk_count
FROM information_schema.table_constraints
WHERE constraint_type = 'FOREIGN KEY'
    AND table_schema IN ('platform_schema', 'aia_schema', 'pkb_schema')
GROUP BY table_schema
ORDER BY table_schema;

-- 6. 索引统计
SELECT '=== 8. 索引统计 ===' as section;
SELECT 
    schemaname,
    COUNT(*) as index_count
FROM pg_indexes
WHERE schemaname IN ('platform_schema', 'aia_schema', 'pkb_schema')
GROUP BY schemaname
ORDER BY schemaname;

-- 7. 数据采样
SELECT '=== 9. 数据采样（platform_schema.users 前3条） ===' as section;
SELECT id, email, name, role FROM platform_schema.users ORDER BY created_at DESC LIMIT 3;

SELECT '=== 10. 数据采样（aia_schema.projects 前3条） ===' as section;
SELECT id, name, research_type FROM aia_schema.projects ORDER BY created_at DESC LIMIT 3;

SELECT '=== 11. 数据采样（pkb_schema.knowledge_bases 前3条） ===' as section;
SELECT id, name, file_count FROM pkb_schema.knowledge_bases ORDER BY created_at DESC LIMIT 3;

-- 最终总结
SELECT '=== 验证完成！✅ ===' as final_summary;

