-- ============================================================================
-- 数据库脚本验证查询
-- 用于验证 mysql_emotion_museum_final.sql 执行后的表结构
-- ============================================================================

-- 验证数据库是否存在
SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'emotion_museum';

-- 验证所有表是否创建成功
SELECT TABLE_NAME, TABLE_COMMENT 
FROM INFORMATION_SCHEMA.TABLES 
WHERE TABLE_SCHEMA = 'emotion_museum' 
ORDER BY TABLE_NAME;

-- 验证conversation表的字段结构（重点验证新增字段）
SELECT 
    COLUMN_NAME,
    DATA_TYPE,
    IS_NULLABLE,
    COLUMN_DEFAULT,
    COLUMN_COMMENT
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_SCHEMA = 'emotion_museum' 
  AND TABLE_NAME = 'conversation'
ORDER BY ORDINAL_POSITION;

-- 验证conversation表的索引
SELECT 
    INDEX_NAME,
    COLUMN_NAME,
    NON_UNIQUE
FROM INFORMATION_SCHEMA.STATISTICS 
WHERE TABLE_SCHEMA = 'emotion_museum' 
  AND TABLE_NAME = 'conversation'
ORDER BY INDEX_NAME, SEQ_IN_INDEX;

-- 验证新增字段是否存在
SELECT 
    CASE 
        WHEN COUNT(*) = 9 THEN '✅ 所有新增字段都存在'
        ELSE CONCAT('❌ 缺少字段，只找到 ', COUNT(*), ' 个，应该是 9 个')
    END AS validation_result
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_SCHEMA = 'emotion_museum' 
  AND TABLE_NAME = 'conversation'
  AND COLUMN_NAME IN (
    'user_type', 'emotion_trend', 'keywords', 'ai_insights', 
    'confidence', 'client_ip', 'user_agent', 'summary', 'tags'
  );

-- 验证新增索引是否存在
SELECT 
    CASE 
        WHEN COUNT(*) = 4 THEN '✅ 所有新增索引都存在'
        ELSE CONCAT('❌ 缺少索引，只找到 ', COUNT(*), ' 个，应该是 4 个')
    END AS index_validation_result
FROM INFORMATION_SCHEMA.STATISTICS 
WHERE TABLE_SCHEMA = 'emotion_museum' 
  AND TABLE_NAME = 'conversation'
  AND INDEX_NAME IN (
    'idx_conversation_user_type',
    'idx_conversation_emotion_trend', 
    'idx_conversation_confidence',
    'idx_conversation_client_ip'
  );

-- 统计总表数
SELECT 
    CASE 
        WHEN COUNT(*) = 15 THEN '✅ 所有15个表都创建成功'
        ELSE CONCAT('❌ 表数量不正确，只有 ', COUNT(*), ' 个表，应该是 15 个')
    END AS table_count_result
FROM INFORMATION_SCHEMA.TABLES 
WHERE TABLE_SCHEMA = 'emotion_museum';

-- 统计总索引数（conversation表）
SELECT 
    CONCAT('conversation表共有 ', COUNT(DISTINCT INDEX_NAME), ' 个索引') AS conversation_index_count
FROM INFORMATION_SCHEMA.STATISTICS 
WHERE TABLE_SCHEMA = 'emotion_museum' 
  AND TABLE_NAME = 'conversation';
