# ============================================================================
# PrometheusRule - Custom Alerting Rules for Undateable
# ============================================================================

apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
  name: undateable-alerts
  namespace: undateable
  labels:
    app: undateable
    release: prometheus
spec:
  groups:
    # ========================================================================
    # Service Health Alerts
    # ========================================================================
    - name: undateable.service.health
      rules:
        - alert: ServiceDown
          expr: up{namespace="undateable"} == 0
          for: 1m
          labels:
            severity: critical
          annotations:
            summary: "Service {{ $labels.job }} is down"
            description: "{{ $labels.job }} has been down for more than 1 minute."

        - alert: ServiceHighErrorRate
          expr: |
            sum(rate(http_requests_total{namespace="undateable", status=~"5.."}[5m])) by (app)
            /
            sum(rate(http_requests_total{namespace="undateable"}[5m])) by (app)
            > 0.05
          for: 5m
          labels:
            severity: warning
          annotations:
            summary: "High error rate on {{ $labels.app }}"
            description: "{{ $labels.app }} has >5% error rate for 5 minutes."

        - alert: ServiceHighLatency
          expr: |
            histogram_quantile(0.95,
              sum(rate(http_request_duration_seconds_bucket{namespace="undateable"}[5m])) by (le, app)
            ) > 2
          for: 5m
          labels:
            severity: warning
          annotations:
            summary: "High latency on {{ $labels.app }}"
            description: "{{ $labels.app }} p95 latency is above 2 seconds."

    # ========================================================================
    # Pod Alerts
    # ========================================================================
    - name: undateable.pods
      rules:
        - alert: PodCrashLooping
          expr: |
            increase(kube_pod_container_status_restarts_total{namespace="undateable"}[1h]) > 5
          for: 5m
          labels:
            severity: warning
          annotations:
            summary: "Pod {{ $labels.pod }} is crash looping"
            description: "Pod {{ $labels.pod }} has restarted more than 5 times in the last hour."

        - alert: PodNotReady
          expr: |
            kube_pod_status_ready{namespace="undateable", condition="true"} == 0
          for: 5m
          labels:
            severity: warning
          annotations:
            summary: "Pod {{ $labels.pod }} is not ready"
            description: "Pod {{ $labels.pod }} has been not ready for more than 5 minutes."

        - alert: PodHighMemory
          expr: |
            container_memory_usage_bytes{namespace="undateable"}
            /
            container_spec_memory_limit_bytes{namespace="undateable"}
            > 0.9
          for: 5m
          labels:
            severity: warning
          annotations:
            summary: "High memory usage in {{ $labels.pod }}"
            description: "Pod {{ $labels.pod }} is using >90% of its memory limit."

        - alert: PodHighCPU
          expr: |
            sum(rate(container_cpu_usage_seconds_total{namespace="undateable"}[5m])) by (pod)
            /
            sum(container_spec_cpu_quota{namespace="undateable"}/container_spec_cpu_period{namespace="undateable"}) by (pod)
            > 0.9
          for: 5m
          labels:
            severity: warning
          annotations:
            summary: "High CPU usage in {{ $labels.pod }}"
            description: "Pod {{ $labels.pod }} is using >90% of its CPU limit."

    # ========================================================================
    # Database Alerts (if MySQL exporter is configured)
    # ========================================================================
    - name: undateable.database
      rules:
        - alert: DatabaseConnectionsHigh
          expr: mysql_global_status_threads_connected > 50
          for: 5m
          labels:
            severity: warning
          annotations:
            summary: "High number of database connections"
            description: "MySQL has more than 50 active connections."

        - alert: DatabaseSlowQueries
          expr: rate(mysql_global_status_slow_queries[5m]) > 0.1
          for: 5m
          labels:
            severity: warning
          annotations:
            summary: "Database slow queries detected"
            description: "MySQL is experiencing slow queries."

    # ========================================================================
    # Ingress Alerts
    # ========================================================================
    - name: undateable.ingress
      rules:
        - alert: IngressHighErrorRate
          expr: |
            sum(rate(nginx_ingress_controller_requests{status=~"5.."}[5m]))
            /
            sum(rate(nginx_ingress_controller_requests[5m]))
            > 0.05
          for: 5m
          labels:
            severity: warning
          annotations:
            summary: "High ingress error rate"
            description: "NGINX Ingress is returning >5% 5xx errors."

        - alert: IngressHighLatency
          expr: |
            histogram_quantile(0.95,
              sum(rate(nginx_ingress_controller_request_duration_seconds_bucket[5m])) by (le)
            ) > 5
          for: 5m
          labels:
            severity: warning
          annotations:
            summary: "High ingress latency"
            description: "NGINX Ingress p95 latency is above 5 seconds."

    # ========================================================================
    # Certificate Alerts
    # ========================================================================
    - name: undateable.certificates
      rules:
        - alert: CertificateExpiringSoon
          expr: |
            (certmanager_certificate_expiration_timestamp_seconds - time()) / 86400 < 14
          for: 1h
          labels:
            severity: warning
          annotations:
            summary: "Certificate {{ $labels.name }} expiring soon"
            description: "Certificate {{ $labels.name }} will expire in less than 14 days."

        - alert: CertificateExpiryCritical
          expr: |
            (certmanager_certificate_expiration_timestamp_seconds - time()) / 86400 < 7
          for: 1h
          labels:
            severity: critical
          annotations:
            summary: "Certificate {{ $labels.name }} expiring very soon"
            description: "Certificate {{ $labels.name }} will expire in less than 7 days!"
