-- ============================================================
-- Production Database Upgrade Script (Simplified)
-- ============================================================
-- 目标：仅更新菜单相关数据
-- 1. 添加 celestial_bodies.short_name 字段
-- 2. 更新 menus 表
-- 3. 更新 role_menus 表
-- ============================================================

-- 开启"上帝模式"：忽略外键约束和触发器
SET session_replication_role = 'replica';

BEGIN;

-- ============================================================
-- 1. Add short_name column to celestial_bodies
-- ============================================================
DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.columns
        WHERE table_name = 'celestial_bodies'
        AND column_name = 'short_name'
    ) THEN
        ALTER TABLE celestial_bodies ADD COLUMN short_name VARCHAR(50);
        RAISE NOTICE 'Added short_name column to celestial_bodies';
    ELSE
        RAISE NOTICE 'short_name column already exists';
    END IF;
END $$;

-- ============================================================
-- 2. 更新 menus 表
-- ============================================================

-- 清空现有菜单数据
TRUNCATE TABLE menus CASCADE;

-- 插入新的菜单数据
INSERT INTO menus (id, parent_id, name, title, icon, path, component, sort_order, is_active, description, created_at, updated_at) VALUES
(1, NULL, 'dashboard', '控制台', 'dashboard', '/admin/dashboard', 'admin/Dashboard', 1, true, '系统控制台', '2025-11-28 18:07:11.767382', '2025-11-28 18:07:11.767382'),
(2, NULL, 'data_management', '数据管理', 'database', '', '', 2, true, '数据管理模块', '2025-11-28 18:07:11.767382', '2025-11-28 18:07:11.767382'),
(6, NULL, 'platform_management', '平台管理', 'settings', '', '', 3, true, '管理用户和系统参数', '2025-11-29 19:03:08.776597', '2025-11-29 19:03:08.776597'),
(14, NULL, 'user_profile', '个人资料', 'profile', '/user/profile', 'user/Profile', 1, true, '个人资料管理', '2025-12-18 16:26:11.778475', '2025-12-18 16:26:11.778475'),
(15, NULL, 'user_follow', '我的天体', 'star', '/user/follow', 'user/UserFollow', 2, true, '我关注的天体', '2025-12-18 16:27:48.688747', '2025-12-18 16:27:48.688747'),
(11, 2, 'star_systems', '恒星系统管理', 'StarOutlined', '/admin/star-systems', 'StarSystems', 1, true, '管理太阳系和系外恒星系统', '2025-12-06 02:35:21.137234', '2025-12-06 02:35:21.137234'),
(3, 2, 'celestial_bodies', '天体数据管理', NULL, '/admin/celestial-bodies', 'admin/CelestialBodies', 2, true, '查看和管理天体数据', '2025-11-28 18:07:11.767382', '2025-11-28 18:07:11.767382'),
(4, 2, 'static_data', '静态数据管理', NULL, '/admin/static-data', 'admin/StaticData', 2, true, '查看和管理静态数据（星座、星系等）', '2025-11-28 18:07:11.767382', '2025-11-28 18:07:11.767382'),
(5, 2, 'nasa_data', 'Horizon数据下载', NULL, '/admin/nasa-data', 'admin/NasaData', 3, true, '管理NASA Horizons数据下载', '2025-11-28 18:07:11.767382', '2025-11-28 18:07:11.767382'),
(13, 2, 'celestial_events', '天体事件', 'CalendarOutlined', '/admin/celestial-events', '', 4, true, '', '2025-12-15 03:20:39.798021', '2025-12-15 03:20:39.798021'),
(7, 6, 'user_management', '用户管理', NULL, '/admin/users', 'admin/Users', 1, true, '管理系统用户账号', '2025-11-29 19:03:08.776597', '2025-11-29 19:03:08.776597'),
(8, 6, 'platform_parameters_management', '平台参数管理', NULL, '/admin/settings', 'admin/Settings', 2, true, '管理系统通用配置参数', '2025-11-29 19:03:08.776597', '2025-11-29 19:03:08.776597'),
(12, 6, 'scheduled_jobs', '定时任务设置', 'ClockCircleOutlined', '/admin/scheduled-jobs', 'admin/ScheduledJobs', 5, true, '管理系统定时任务及脚本', '2025-12-10 17:42:38.031518', '2025-12-10 17:42:38.031518'),
(10, 6, 'system_tasks', '系统任务监控', 'schedule', '/admin/tasks', 'admin/Tasks', 30, true, '', '2025-11-30 16:04:59.572869', '2025-11-30 16:04:59.572869');

-- Reset sequence for menus
SELECT setval('menus_id_seq', (SELECT MAX(id) FROM menus));

-- ============================================================
-- 3. 更新 role_menus 表
-- ============================================================

-- 获取 admin 和 user 角色的实际 ID（因为生产环境是 3 和 4）
DO $$
DECLARE
    admin_role_id INTEGER;
    user_role_id INTEGER;
BEGIN
    -- 查找 admin 角色 ID
    SELECT id INTO admin_role_id FROM roles WHERE name = 'admin' LIMIT 1;
    -- 查找 user 角色 ID
    SELECT id INTO user_role_id FROM roles WHERE name = 'user' LIMIT 1;

    IF admin_role_id IS NULL OR user_role_id IS NULL THEN
        RAISE EXCEPTION 'Cannot find admin or user role';
    END IF;

    -- 插入 role_menus（使用实际的 role_id）
    -- Admin role 可以访问所有菜单
    INSERT INTO role_menus (role_id, menu_id) VALUES
    (admin_role_id, 1), (admin_role_id, 2), (admin_role_id, 3), (admin_role_id, 4),
    (admin_role_id, 5), (admin_role_id, 6), (admin_role_id, 7), (admin_role_id, 8),
    (admin_role_id, 10), (admin_role_id, 11), (admin_role_id, 12), (admin_role_id, 13),
    (admin_role_id, 14), (admin_role_id, 15);

    -- User role 只能访问用户菜单
    INSERT INTO role_menus (role_id, menu_id) VALUES
    (user_role_id, 14), (user_role_id, 15);

    RAISE NOTICE 'Inserted role_menus for admin_role_id=% and user_role_id=%', admin_role_id, user_role_id;
END $$;

-- ============================================================
-- 提交事务
-- ============================================================

COMMIT;

-- ============================================================
-- 恢复正常模式
-- ============================================================

SET session_replication_role = 'origin';

-- ============================================================
-- 验证结果
-- ============================================================

-- 检查 short_name 字段
SELECT 'celestial_bodies.short_name' as "Item",
       CASE WHEN EXISTS(
           SELECT 1 FROM information_schema.columns
           WHERE table_name='celestial_bodies' AND column_name='short_name'
       ) THEN '✓ EXISTS' ELSE '✗ MISSING' END as "Status";

-- 检查记录数
SELECT 'menus' as "Table", COUNT(*)::text || ' records' as "Count" FROM menus
UNION ALL
SELECT 'role_menus', COUNT(*)::text || ' records' FROM role_menus;

-- 检查角色菜单分配
SELECT r.name as role, COUNT(rm.menu_id) as menu_count
FROM roles r
LEFT JOIN role_menus rm ON r.id = rm.role_id
GROUP BY r.id, r.name
ORDER BY r.name;
