-- ============================================================
-- M21 运营后台数据库菜单
--   菜单、层级、角色可见性由数据库驱动，前端只负责渲染菜单树。
-- ============================================================

CREATE TABLE admin_menus
(
    id            BIGINT       NOT NULL,
    parent_id     BIGINT       NOT NULL DEFAULT 0,
    title         VARCHAR(64)  NOT NULL,
    path          VARCHAR(128)          DEFAULT NULL,
    icon          VARCHAR(64)           DEFAULT NULL,
    allowed_roles VARCHAR(255)          DEFAULT NULL COMMENT '逗号分隔；NULL 表示所有登录运营角色可见',
    sort_order    INT          NOT NULL DEFAULT 0,
    status        TINYINT      NOT NULL DEFAULT 1 COMMENT '1=启用 0=隐藏',
    created_at    DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    updated_at    DATETIME(3)  NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
    PRIMARY KEY (id),
    KEY idx_parent_sort (parent_id, sort_order),
    KEY idx_status_sort (status, sort_order)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COMMENT '运营后台菜单';

INSERT INTO admin_menus (id, parent_id, title, path, icon, allowed_roles, sort_order, status)
VALUES
    (100, 0, '仪表盘', '/dashboard', 'DataBoard', NULL, 100, 1),
    (200, 0, '租户管理', NULL, 'OfficeBuilding', NULL, 200, 1),
    (201, 200, '企业列表', '/tenants', 'OfficeBuilding', 'SUPER_ADMIN,OPERATOR,FINANCE,READONLY', 201, 1),
    (202, 200, '组织员工', '/users', 'UserFilled', 'SUPER_ADMIN,OPERATOR,READONLY', 202, 1),
    (203, 200, '审核中心', '/tenant-audits', 'Finished', 'SUPER_ADMIN,OPERATOR', 203, 1),
    (300, 0, '计费与订单', NULL, 'Wallet', NULL, 300, 1),
    (310, 300, '计费与套餐', NULL, 'Coin', NULL, 310, 1),
    (311, 310, '计费记录', '/billing/records', 'Coin', 'SUPER_ADMIN,OPERATOR,FINANCE,READONLY', 311, 1),
    (312, 310, '计费规则', '/billing/costs', 'Money', 'SUPER_ADMIN', 312, 1),
    (313, 310, '套餐管理', '/plans', 'Wallet', 'SUPER_ADMIN', 313, 1),
    (320, 300, '订单发票', NULL, 'CreditCard', 'SUPER_ADMIN,FINANCE,OPERATOR', 320, 1),
    (321, 320, '订单与发票', '/orders-invoices', 'CreditCard', 'SUPER_ADMIN,FINANCE,OPERATOR', 321, 1),
    (400, 0, '数据与预警', NULL, 'DataAnalysis', NULL, 400, 1),
    (401, 400, '运营预警', '/warnings', 'Warning', 'SUPER_ADMIN,OPERATOR,READONLY', 401, 1),
    (402, 400, '数据报表', '/reports', 'Histogram', 'SUPER_ADMIN,OPERATOR,FINANCE,READONLY', 402, 1),
    (500, 0, '客户服务', NULL, 'Headset', 'SUPER_ADMIN,OPERATOR,READONLY', 500, 1),
    (501, 500, '工单工作台', '/tickets', 'Tickets', 'SUPER_ADMIN,OPERATOR', 501, 1),
    (502, 500, '话术库', '/script-library', 'ChatDotSquare', 'SUPER_ADMIN,OPERATOR', 502, 1),
    (503, 500, '客服绩效看板', '/support-performance', 'TrendCharts', 'SUPER_ADMIN,OPERATOR,READONLY', 503, 1),
    (600, 0, '内容运营', NULL, 'Collection', 'SUPER_ADMIN,OPERATOR,READONLY', 600, 1),
    (601, 600, '使用手册', '/manuals', 'Guide', 'SUPER_ADMIN,OPERATOR', 601, 1),
    (602, 600, '政策与新闻', '/announcements', 'Bell', 'SUPER_ADMIN,OPERATOR', 602, 1),
    (700, 0, '系统设置', NULL, 'Setting', NULL, 700, 1),
    (710, 700, '权限与安全', NULL, 'Key', 'SUPER_ADMIN,READONLY', 710, 1),
    (711, 710, '权限管理', '/admin-users', 'Key', 'SUPER_ADMIN', 711, 1),
    (712, 710, '操作日志', '/audit-logs', 'Memo', 'SUPER_ADMIN,READONLY', 712, 1),
    (720, 700, '平台配置', NULL, 'SetUp', 'SUPER_ADMIN', 720, 1),
    (721, 720, '全局参数', '/global-settings', 'SetUp', 'SUPER_ADMIN', 721, 1)
ON DUPLICATE KEY UPDATE
    parent_id = VALUES(parent_id),
    title = VALUES(title),
    path = VALUES(path),
    icon = VALUES(icon),
    allowed_roles = VALUES(allowed_roles),
    sort_order = VALUES(sort_order),
    status = VALUES(status);
