CREATE TABLE IF NOT EXISTS notification_task (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    tenant_id BIGINT UNSIGNED NOT NULL,
    policy_id BIGINT UNSIGNED NOT NULL,
    event_code VARCHAR(64) NOT NULL,
    channel VARCHAR(32) NOT NULL,
    receiver_type VARCHAR(32) NOT NULL,
    receiver_ref VARCHAR(128) DEFAULT NULL,
    payload_json TEXT,
    status VARCHAR(32) NOT NULL DEFAULT 'PENDING',
    retry_count INT NOT NULL DEFAULT 0,
    max_retry INT NOT NULL DEFAULT 3,
    error_message VARCHAR(500) DEFAULT NULL,
    idempotency_key VARCHAR(128) DEFAULT NULL,
    sent_at DATETIME 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_tenant_idempotency (tenant_id, idempotency_key),
    KEY idx_tenant_event_status (tenant_id, event_code, status),
    KEY idx_tenant_created (tenant_id, created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS export_task (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    tenant_id BIGINT UNSIGNED NOT NULL,
    task_code VARCHAR(64) NOT NULL,
    biz_type VARCHAR(64) NOT NULL,
    biz_id VARCHAR(128) DEFAULT NULL,
    filters_json TEXT,
    file_name VARCHAR(255) NOT NULL,
    file_oss_key VARCHAR(512) DEFAULT NULL,
    status VARCHAR(32) NOT NULL DEFAULT 'PENDING',
    retry_count INT NOT NULL DEFAULT 0,
    max_retry INT NOT NULL DEFAULT 3,
    error_message VARCHAR(500) DEFAULT NULL,
    idempotency_key VARCHAR(128) DEFAULT NULL,
    requested_by BIGINT UNSIGNED NOT NULL DEFAULT 0,
    finished_at DATETIME 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_tenant_export_idempotency (tenant_id, idempotency_key),
    KEY idx_tenant_status_time (tenant_id, status, created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

ALTER TABLE alert_event
    ADD COLUMN recover_candidate_at DATETIME DEFAULT NULL AFTER recovered_at;

INSERT INTO permission (id, permission_code, permission_name, module)
VALUES
    (1066, 'notification.dispatch', '触发通知发送', 'notification'),
    (1067, 'notification.task.read', '查看通知发送任务', 'notification'),
    (1068, 'export.task.read', '查看导出任务', 'export'),
    (1069, 'export.task.manage', '创建导出任务', 'export')
ON DUPLICATE KEY UPDATE permission_name = VALUES(permission_name), module = VALUES(module);

INSERT INTO role_permission (id, tenant_id, role_id, permission_id)
VALUES
    (75, 1, 101, 1066),
    (76, 1, 101, 1067),
    (77, 1, 101, 1068),
    (78, 1, 101, 1069)
ON DUPLICATE KEY UPDATE permission_id = VALUES(permission_id);
