CREATE TABLE IF NOT EXISTS platform_notify_gateway (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    channel_code VARCHAR(32) NOT NULL,
    gateway_name VARCHAR(64) NOT NULL,
    provider_code VARCHAR(64) NOT NULL,
    status VARCHAR(32) NOT NULL DEFAULT 'DISABLED',
    config_json TEXT DEFAULT NULL,
    secret_config_cipher TEXT DEFAULT NULL,
    remark VARCHAR(255) DEFAULT NULL,
    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_platform_notify_gateway_channel (channel_code)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO platform_notify_gateway (
    channel_code,
    gateway_name,
    provider_code,
    status,
    config_json,
    secret_config_cipher,
    remark,
    is_deleted,
    created_by,
    updated_by
)
SELECT 'EMAIL', '邮件网关', 'SMTP', 'DISABLED', '{}', '', '平台统一邮件网关配置', 0, 0, 0
WHERE NOT EXISTS (
    SELECT 1 FROM platform_notify_gateway WHERE channel_code = 'EMAIL'
);

INSERT INTO platform_notify_gateway (
    channel_code,
    gateway_name,
    provider_code,
    status,
    config_json,
    secret_config_cipher,
    remark,
    is_deleted,
    created_by,
    updated_by
)
SELECT 'SMS', '短信网关', 'MOCK', 'DISABLED', '{}', '', '平台统一短信网关配置', 0, 0, 0
WHERE NOT EXISTS (
    SELECT 1 FROM platform_notify_gateway WHERE channel_code = 'SMS'
);

INSERT INTO platform_permission (permission_code, permission_name, module)
SELECT 'platform.notify-gateway.read', '平台通知网关查看', 'platform'
WHERE NOT EXISTS (
    SELECT 1 FROM platform_permission WHERE permission_code = 'platform.notify-gateway.read'
);

INSERT INTO platform_permission (permission_code, permission_name, module)
SELECT 'platform.notify-gateway.manage', '平台通知网关管理', 'platform'
WHERE NOT EXISTS (
    SELECT 1 FROM platform_permission WHERE permission_code = 'platform.notify-gateway.manage'
);

UPDATE platform_permission
SET permission_name = '平台通知网关查看', module = 'platform'
WHERE permission_code = 'platform.notify-gateway.read';

UPDATE platform_permission
SET permission_name = '平台通知网关管理', module = 'platform'
WHERE permission_code = 'platform.notify-gateway.manage';

INSERT INTO platform_role_permission (role_id, permission_id)
SELECT r.id, p.id
FROM platform_role r
JOIN platform_permission p ON p.permission_code = 'platform.notify-gateway.read'
LEFT JOIN platform_role_permission rp ON rp.role_id = r.id AND rp.permission_id = p.id
WHERE r.role_code = 'PLATFORM_SUPER_ADMIN'
  AND r.is_deleted = 0
  AND rp.id IS NULL;

INSERT INTO platform_role_permission (role_id, permission_id)
SELECT r.id, p.id
FROM platform_role r
JOIN platform_permission p ON p.permission_code = 'platform.notify-gateway.manage'
LEFT JOIN platform_role_permission rp ON rp.role_id = r.id AND rp.permission_id = p.id
WHERE r.role_code = 'PLATFORM_SUPER_ADMIN'
  AND r.is_deleted = 0
  AND rp.id IS NULL;

INSERT INTO platform_menu (
    menu_code,
    menu_name,
    route_path,
    permission_code,
    sort_no,
    status,
    is_deleted,
    created_by,
    updated_by
)
SELECT 'platform_notify_gateway', '通知网关配置', '/platform/notify-gateways', 'platform.notify-gateway.read', 65, 'ENABLED', 0, 0, 0
WHERE NOT EXISTS (
    SELECT 1 FROM platform_menu WHERE menu_code = 'platform_notify_gateway'
);

INSERT INTO platform_role_menu (role_id, menu_id)
SELECT r.id, m.id
FROM platform_role r
JOIN platform_menu m ON m.menu_code = 'platform_notify_gateway'
LEFT JOIN platform_role_menu rm ON rm.role_id = r.id AND rm.menu_id = m.id
WHERE r.role_code = 'PLATFORM_SUPER_ADMIN'
  AND r.is_deleted = 0
  AND rm.id IS NULL;
