CREATE TABLE IF NOT EXISTS menu (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    tenant_id BIGINT UNSIGNED NOT NULL,
    menu_code VARCHAR(64) NOT NULL,
    menu_name VARCHAR(64) NOT NULL,
    route_path VARCHAR(128) NOT NULL,
    sort_no INT NOT NULL DEFAULT 100,
    status VARCHAR(32) NOT NULL DEFAULT 'ENABLED',
    is_deleted TINYINT(1) NOT NULL DEFAULT 0,
    created_by BIGINT UNSIGNED NOT NULL DEFAULT 0,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_by BIGINT UNSIGNED NOT NULL DEFAULT 0,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE KEY uk_tenant_menu_code (tenant_id, menu_code),
    KEY idx_tenant_status_sort (tenant_id, status, sort_no)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS role_menu (
    id BIGINT UNSIGNED NOT NULL PRIMARY KEY,
    tenant_id BIGINT UNSIGNED NOT NULL,
    role_id BIGINT UNSIGNED NOT NULL,
    menu_id BIGINT UNSIGNED NOT NULL,
    UNIQUE KEY uk_tenant_role_menu (tenant_id, role_id, menu_id),
    KEY idx_tenant_role (tenant_id, role_id),
    KEY idx_tenant_menu (tenant_id, menu_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO menu (tenant_id, menu_code, menu_name, route_path, sort_no, status, is_deleted, created_by, updated_by)
VALUES
    (1, 'project', '项目管理', '/projects', 10, 'ENABLED', 0, 0, 0),
    (1, 'meeting', '会议管理', '/meetings', 20, 'ENABLED', 0, 0, 0),
    (1, 'audit', '审核管理', '/audits', 30, 'ENABLED', 0, 0, 0),
    (1, 'finance', '财务管理', '/finance', 40, 'ENABLED', 0, 0, 0),
    (1, 'user', '用户管理', '/users', 50, 'ENABLED', 0, 0, 0),
    (1, 'tenant', '租户管理', '/tenants', 60, 'ENABLED', 0, 0, 0),
    (1, 'enterprise', '企业管理', '/enterprises', 70, 'ENABLED', 0, 0, 0),
    (1, 'role', '角色管理', '/roles', 80, 'ENABLED', 0, 0, 0),
    (1, 'menu', '菜单管理', '/menus', 90, 'ENABLED', 0, 0, 0),
    (1, 'audit_flow', '审核流配置', '/audit-flows', 100, 'ENABLED', 0, 0, 0),
    (1, 'data_permission', '数据权限管理', '/data-permissions', 110, 'ENABLED', 0, 0, 0),
    (1, 'template', '模板管理', '/templates', 120, 'ENABLED', 0, 0, 0),
    (1, 'template_download_log', '模板下载日志', '/template-download-logs', 130, 'ENABLED', 0, 0, 0),
    (1, 'audit_log', '审计日志', '/audit-logs', 140, 'ENABLED', 0, 0, 0),
    (1, 'expert', '专家管理', '/experts', 150, 'ENABLED', 0, 0, 0),
    (1, 'meeting_field', '会议字段管理', '/meeting-fields', 160, 'ENABLED', 0, 0, 0),
    (1, 'invoice_profile', '发票管理', '/invoice-profiles', 170, 'ENABLED', 0, 0, 0),
    (1, 'notification_policy', '通知策略', '/notification-policies', 180, 'ENABLED', 0, 0, 0),
    (1, 'export_task', '导出任务中心', '/export-tasks', 190, 'ENABLED', 0, 0, 0),
    (1, 'observability', '可观测性', '/observability', 200, 'ENABLED', 0, 0, 0),
    (1, 'operations_dashboard', '运营看板', '/operations-dashboard', 210, 'ENABLED', 0, 0, 0)
ON DUPLICATE KEY UPDATE
    menu_name = VALUES(menu_name),
    route_path = VALUES(route_path),
    sort_no = VALUES(sort_no),
    status = VALUES(status),
    is_deleted = VALUES(is_deleted),
    updated_at = CURRENT_TIMESTAMP;

INSERT INTO role_menu (id, tenant_id, role_id, menu_id)
SELECT
    (1000 + m.id) AS id,
    1 AS tenant_id,
    101 AS role_id,
    m.id AS menu_id
FROM menu m
WHERE m.tenant_id = 1
ON DUPLICATE KEY UPDATE menu_id = VALUES(menu_id);
