CREATE TABLE IF NOT EXISTS observability_metric (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    tenant_id BIGINT UNSIGNED NOT NULL,
    metric_code VARCHAR(64) NOT NULL,
    label_key VARCHAR(64) DEFAULT NULL,
    label_value VARCHAR(255) DEFAULT NULL,
    metric_value DOUBLE NOT NULL DEFAULT 0,
    created_by BIGINT UNSIGNED NOT NULL DEFAULT 0,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    KEY idx_tenant_metric_time (tenant_id, metric_code, created_at),
    KEY idx_tenant_label (tenant_id, label_key, label_value)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS alert_rule (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    tenant_id BIGINT UNSIGNED NOT NULL,
    rule_code VARCHAR(64) NOT NULL,
    rule_name VARCHAR(128) NOT NULL,
    compare_op VARCHAR(8) NOT NULL DEFAULT '>=',
    threshold_value DOUBLE NOT NULL DEFAULT 0,
    window_minute INT NOT NULL DEFAULT 5,
    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_rule_code (tenant_id, rule_code)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS alert_event (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    tenant_id BIGINT UNSIGNED NOT NULL,
    rule_code VARCHAR(64) NOT NULL,
    metric_code VARCHAR(64) NOT NULL,
    current_value DOUBLE NOT NULL DEFAULT 0,
    threshold_value DOUBLE NOT NULL DEFAULT 0,
    alert_level VARCHAR(32) NOT NULL DEFAULT 'WARN',
    message VARCHAR(500) DEFAULT NULL,
    created_by BIGINT UNSIGNED NOT NULL DEFAULT 0,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    KEY idx_tenant_rule_time (tenant_id, rule_code, created_at),
    KEY idx_tenant_metric_time (tenant_id, metric_code, created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO alert_rule (tenant_id, rule_code, rule_name, compare_op, threshold_value, window_minute, status, created_by, updated_by)
VALUES
    (1, 'API_5XX_RATE', 'API 5xx错误率', '>=', 5, 5, 'ENABLED', 0, 0),
    (1, 'ASYNC_BACKLOG', '异步任务积压量', '>=', 50, 5, 'ENABLED', 0, 0),
    (1, 'ASYNC_FAILED_RATE', '异步任务失败率', '>=', 10, 10, 'ENABLED', 0, 0)
ON DUPLICATE KEY UPDATE threshold_value = VALUES(threshold_value), window_minute = VALUES(window_minute), updated_at = CURRENT_TIMESTAMP;

INSERT INTO permission (id, permission_code, permission_name, module)
VALUES
    (1059, 'observability.read', '查看可观测性指标', 'observability'),
    (1060, 'observability.manage', '管理可观测性告警', 'observability')
ON DUPLICATE KEY UPDATE permission_name = VALUES(permission_name), module = VALUES(module);

INSERT INTO role_permission (id, tenant_id, role_id, permission_id)
VALUES
    (60, 1, 101, 1059),
    (61, 1, 101, 1060)
ON DUPLICATE KEY UPDATE permission_id = VALUES(permission_id);
