-- 针对现有表的规范化脚本
-- 基于实际数据库状态

USE tg_manage;

-- 禁用外键检查
SET FOREIGN_KEY_CHECKS = 0;

-- 显示当前表状态
SELECT 'Current tables before normalization:' as status;
SHOW TABLES;

-- 重命名tg_前缀的表（只处理存在的表）
-- 账号相关
RENAME TABLE IF EXISTS tg_account TO accounts_new;
RENAME TABLE IF EXISTS tg_account_health TO account_health;
RENAME TABLE IF EXISTS tg_account_pool TO account_pools;
RENAME TABLE IF EXISTS tg_account_usage TO account_usages;
RENAME TABLE IF EXISTS tg_account_usage_log TO account_usage_logs;

-- 群组相关
RENAME TABLE IF EXISTS tg_group TO chat_groups_new;
RENAME TABLE IF EXISTS tg_group_listener TO group_listeners;
RENAME TABLE IF EXISTS tg_group_marketing_log TO group_marketing_logs;
RENAME TABLE IF EXISTS tg_group_muster TO group_musters;
RENAME TABLE IF EXISTS tg_group_send_log TO group_send_logs;
RENAME TABLE IF EXISTS tg_group_task TO group_tasks;
RENAME TABLE IF EXISTS tg_group_user TO group_users;

-- 消息相关
RENAME TABLE IF EXISTS tg_message TO messages_new;
RENAME TABLE IF EXISTS tg_message_muster TO message_musters;

-- 脚本相关
RENAME TABLE IF EXISTS tg_script TO scripts_new;
RENAME TABLE IF EXISTS tg_script_article TO script_articles_new;
RENAME TABLE IF EXISTS tg_script_project TO script_projects_new;
RENAME TABLE IF EXISTS tg_script_task TO script_tasks_new;

-- 任务相关
RENAME TABLE IF EXISTS tg_pull_member_task TO pull_member_tasks;
RENAME TABLE IF EXISTS tg_smart_task_execution TO smart_task_executions;
RENAME TABLE IF EXISTS tg_smart_group_task TO smart_group_tasks;

-- 日志相关
RENAME TABLE IF EXISTS tg_login_code_log TO login_code_logs;
RENAME TABLE IF EXISTS tg_register_log TO register_logs;
RENAME TABLE IF EXISTS tg_pull_member_log TO pull_member_logs;
RENAME TABLE IF EXISTS tg_join_group_log TO join_group_logs;
RENAME TABLE IF EXISTS tg_project_invite_log TO project_invite_logs;

-- 统计相关
RENAME TABLE IF EXISTS tg_pull_member_statistic TO pull_member_statistics;
RENAME TABLE IF EXISTS tg_pull_member_project_statistic TO pull_member_project_statistics;

-- 系统配置相关
RENAME TABLE IF EXISTS tg_config TO configs_new;
RENAME TABLE IF EXISTS tg_dc TO data_centers_new;
RENAME TABLE IF EXISTS tg_exchange TO exchanges;
RENAME TABLE IF EXISTS tg_api_data TO api_data;

-- 其他
RENAME TABLE IF EXISTS tg_telegram_users TO telegram_users_new;
RENAME TABLE IF EXISTS tg_user TO users;
RENAME TABLE IF EXISTS tg_lines TO lines;
RENAME TABLE IF EXISTS tg_performer TO performers;

-- 清理c_前缀的旧表
DROP TABLE IF EXISTS c_account_usage;
DROP TABLE IF EXISTS c_api_data;
DROP TABLE IF EXISTS c_config;
DROP TABLE IF EXISTS c_dc;
DROP TABLE IF EXISTS c_exchange;
DROP TABLE IF EXISTS c_firstname;
DROP TABLE IF EXISTS c_group;
DROP TABLE IF EXISTS c_group_listener;
DROP TABLE IF EXISTS c_group_muster;
DROP TABLE IF EXISTS c_group_send_log;
DROP TABLE IF EXISTS c_group_task;
DROP TABLE IF EXISTS c_join_group_log;
DROP TABLE IF EXISTS c_lastname;
DROP TABLE IF EXISTS c_lines;
DROP TABLE IF EXISTS c_message;
DROP TABLE IF EXISTS c_message_muster;
DROP TABLE IF EXISTS c_performer;
DROP TABLE IF EXISTS c_pull_member_log;
DROP TABLE IF EXISTS c_pull_member_project_statistic;
DROP TABLE IF EXISTS c_pull_member_statistic;
DROP TABLE IF EXISTS c_pull_member_task;
DROP TABLE IF EXISTS c_script;
DROP TABLE IF EXISTS c_script_article;
DROP TABLE IF EXISTS c_script_project;
DROP TABLE IF EXISTS c_script_task;
DROP TABLE IF EXISTS c_smart_task;
DROP TABLE IF EXISTS c_task_execution;
DROP TABLE IF EXISTS c_tg_account;
DROP TABLE IF EXISTS c_tg_login_code_log;
DROP TABLE IF EXISTS c_tg_register_log;

-- 清理m_前缀的重复表
DROP TABLE IF EXISTS m_admin;
DROP TABLE IF EXISTS m_api_data;
DROP TABLE IF EXISTS m_config;
DROP TABLE IF EXISTS m_group;
DROP TABLE IF EXISTS m_group_listener;
DROP TABLE IF EXISTS m_group_task;
DROP TABLE IF EXISTS m_message;
DROP TABLE IF EXISTS m_tg_account;

-- 重新启用外键检查
SET FOREIGN_KEY_CHECKS = 1;

-- 显示最终结果
SELECT 'Table normalization completed!' as status;
SHOW TABLES;

-- 显示表记录统计
SELECT 'accounts' as table_name, COUNT(*) as count FROM accounts
UNION ALL
SELECT 'firstnames', COUNT(*) FROM firstnames
UNION ALL
SELECT 'lastnames', COUNT(*) FROM lastnames;