CREATE TABLE IF NOT EXISTS notification_text_template (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    tenant_id BIGINT UNSIGNED NOT NULL,
    template_name VARCHAR(128) NOT NULL,
    subject_template VARCHAR(255) DEFAULT NULL,
    title_template VARCHAR(255) DEFAULT NULL,
    content_template TEXT NOT NULL,
    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_template_name (tenant_id, template_name),
    KEY idx_tenant_status (tenant_id, status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO notification_text_template (tenant_id, template_name, subject_template, title_template, content_template, status, is_deleted, created_by, updated_by)
SELECT 1, '默认通知文案', '系统通知', '系统通知', '您有一条新的系统通知，请及时处理。', 'ENABLED', 0, 0, 0
WHERE NOT EXISTS (
    SELECT 1 FROM notification_text_template WHERE tenant_id=1 AND template_name='默认通知文案' AND is_deleted=0
);

INSERT INTO permission (id, permission_code, permission_name, module)
SELECT t.next_id, 'notification.text-template.read', '查看通知文案模板', 'notification'
FROM (SELECT IFNULL(MAX(id), 0) + 1 AS next_id FROM permission) t
WHERE NOT EXISTS (
    SELECT 1 FROM permission WHERE permission_code = 'notification.text-template.read'
);

INSERT INTO permission (id, permission_code, permission_name, module)
SELECT t.next_id, 'notification.text-template.manage', '管理通知文案模板', 'notification'
FROM (SELECT IFNULL(MAX(id), 0) + 1 AS next_id FROM permission) t
WHERE NOT EXISTS (
    SELECT 1 FROM permission WHERE permission_code = 'notification.text-template.manage'
);

UPDATE permission SET permission_name='查看通知文案模板', module='notification' WHERE permission_code='notification.text-template.read';
UPDATE permission SET permission_name='管理通知文案模板', module='notification' WHERE permission_code='notification.text-template.manage';

INSERT INTO menu (tenant_id, menu_code, menu_name, route_path, permission_code, sort_no, status, is_deleted, created_by, updated_by)
VALUES
    (1, 'notification_text_template', '通知文案模板', '/notification-text-templates', 'notification.text-template.read', 179, 'ENABLED', 0, 0, 0)
ON DUPLICATE KEY UPDATE
    menu_name = VALUES(menu_name),
    route_path = VALUES(route_path),
    permission_code = VALUES(permission_code),
    sort_no = VALUES(sort_no),
    status = VALUES(status),
    is_deleted = VALUES(is_deleted),
    updated_at = CURRENT_TIMESTAMP;

INSERT INTO role_permission (id, tenant_id, role_id, permission_id)
SELECT t.next_id, 1, 101, p.id
FROM (SELECT IFNULL(MAX(id), 0) + 1 AS next_id FROM role_permission) t
JOIN permission p ON p.permission_code = 'notification.text-template.read'
WHERE NOT EXISTS (
    SELECT 1 FROM role_permission rp WHERE rp.tenant_id=1 AND rp.role_id=101 AND rp.permission_id=p.id
);

INSERT INTO role_permission (id, tenant_id, role_id, permission_id)
SELECT t.next_id, 1, 101, p.id
FROM (SELECT IFNULL(MAX(id), 0) + 1 AS next_id FROM role_permission) t
JOIN permission p ON p.permission_code = 'notification.text-template.manage'
WHERE NOT EXISTS (
    SELECT 1 FROM role_permission rp WHERE rp.tenant_id=1 AND rp.role_id=101 AND rp.permission_id=p.id
);

SET @next_role_menu_id = (SELECT IFNULL(MAX(id), 0) FROM role_menu);
INSERT INTO role_menu (id, tenant_id, role_id, menu_id)
SELECT
    (@next_role_menu_id := @next_role_menu_id + 1) AS id,
    r.tenant_id,
    r.id,
    m.id
FROM role r
JOIN menu m ON m.tenant_id=r.tenant_id AND m.menu_code='notification_text_template' AND m.is_deleted=0
WHERE r.role_code='TENANT_ADMIN'
  AND r.is_deleted=0
  AND NOT EXISTS (
    SELECT 1 FROM role_menu rm WHERE rm.tenant_id=r.tenant_id AND rm.role_id=r.id AND rm.menu_id=m.id
  );
