tiku_commercial_health:
  query: |
    SELECT
      (SELECT count(*) FROM worker_heartbeats WHERE last_heartbeat_at < now() - interval '3 minutes')::double precision AS stale_workers,
      COALESCE((SELECT extract(epoch FROM now() - min(created_at)) FROM background_jobs WHERE status = 'pending'), 0)::double precision AS oldest_pending_job_seconds,
      (SELECT count(*) FROM audit_logs WHERE action = 'system_scope.failed' AND created_at >= now() - interval '5 minutes' AND (details ->> 'caller' = 'PlatformBillingNotificationService' OR details ->> 'correlationId' = 'payment-notification'))::double precision AS payment_callback_failures_5m,
      (SELECT count(*) FROM platform_billing_invoices WHERE status = 'overdue')::double precision AS overdue_receivables,
      (SELECT count(*) FROM platform_billing_dunning_notification_events WHERE status = 'failed' AND attempts >= 5)::double precision AS dunning_dead_letters
  metrics:
    - stale_workers:
        usage: GAUGE
        description: Worker heartbeat rows older than three minutes.
    - oldest_pending_job_seconds:
        usage: GAUGE
        description: Age in seconds of the oldest pending PostgreSQL background job.
    - payment_callback_failures_5m:
        usage: GAUGE
        description: Failed SaaS payment callback system scopes during the last five minutes.
    - overdue_receivables:
        usage: GAUGE
        description: Current overdue SaaS receivable count.
    - dunning_dead_letters:
        usage: GAUGE
        description: Dunning notification events failed after all attempts.
