insert into public.platform_audit_alert_rules (
  code, name, description, severity, action_patterns, target_types, conditions, metadata
)
values
  (
    'platform_usage_overage_invoice_created',
    '平台用量超额自动开票',
    '平台按 SaaS 套餐额度和月度用量自动生成超额服务费账单，应进入运营审计台。',
    'medium',
    array['platform.invoice.usage_overage_created', 'platform.invoice.usage_overage_batch_created'],
    array['tenant_invoice', 'tenant_invoice_batch'],
    '{"riskArea":"platform_usage_overage_billing"}'::jsonb,
    '{"defaultRule":true}'::jsonb
  ),
  (
    'platform_usage_overage_worker_failed',
    '平台用量超额开票失败',
    '平台用量超额账单 worker 执行失败，可能导致租户服务费漏收或月度账务不完整。',
    'high',
    array['platform.invoice.usage_overage_worker_failed'],
    array['tenant_invoice_batch'],
    '{"riskArea":"platform_usage_overage_billing","requiresFollowUp":true}'::jsonb,
    '{"defaultRule":true}'::jsonb
  )
on conflict (code) do update
set name = excluded.name,
    description = excluded.description,
    severity = excluded.severity,
    action_patterns = excluded.action_patterns,
    target_types = excluded.target_types,
    conditions = excluded.conditions,
    metadata = public.platform_audit_alert_rules.metadata || excluded.metadata,
    updated_at = now();
