# Custom business-logic monitors for chart freshness metrics.
# Standard Lambda monitors (errors, duration, invocations) are managed by
# the terraform-datadog module in main.tf.

# ── Chart lag alert ────────────────────────────────────────────────────────────
# Fires when lag_alert = 1, meaning FACT_CHARTS has lagged Spotify by more than
# LAG_ALERT_MINUTES (default 15). The Lambda emits this metric every 5 minutes,
# so no_data after 15 min indicates the Lambda itself has stopped running.

resource "datadog_monitor" "chart_lag_alert" {
  name = "${var.environment}-${var.lambda_name}-chart-lag-alert"
  type = "metric alert"
  message = <<-EOT
    {{#is_alert}}
    Spotify daily chart data has not been ingested into FACT_CHARTS within ${var.lag_alert_minutes} minutes of being published by Spotify.
    Check the Chartmetric pipeline.
    ${var.notification_endpoint}
    @incident
    {{/is_alert}}
    {{#is_recovery}}
    Spotify daily chart ingestion has recovered — FACT_CHARTS is back in sync.
    ${var.notification_endpoint}
    {{/is_recovery}}
    {{#is_no_data}}
    ${var.lambda_name} has stopped emitting metrics. Check the EventBridge trigger and Lambda health.
    ${var.notification_endpoint}
    {{/is_no_data}}
  EOT

  query = "max(last_10m):max:insights.spotify_daily_chart.lag_alert{env:${var.environment},service:${var.service_name}}.rollup(max) >= 1"

  monitor_thresholds {
    critical = 1
  }

  notify_no_data      = true
  no_data_timeframe   = 15
  renotify_interval   = 60
  require_full_window = false
  notify_audit        = false
  timeout_h           = 0

  tags = [
    "env:${var.environment}",
    "service:${var.service_name}",
    "application_family:${var.application_family}",
    "team:${var.team_name}",
  ]
}

# ── Spotify API unavailable ────────────────────────────────────────────────────
# Fires when the Spotify Charts provider API has returned no data for more than
# half the last 30-minute window. This metric is only emitted when the Lambda
# is actively checking (not in IDLE state), so notify_no_data is disabled.

resource "datadog_monitor" "spotify_api_unavailable" {
  name = "${var.environment}-${var.lambda_name}-spotify-api-unavailable"
  type = "metric alert"
  message = <<-EOT
    {{#is_alert}}
    The Spotify Charts provider API has been unavailable for more than 15 minutes.
    This is a diagnostic signal — if the chart lag alert is also firing, check Spotify status and the Chartmetric pipeline.
    ${var.notification_endpoint}
    {{/is_alert}}
    {{#is_recovery}}
    Spotify Charts API is responding again.
    ${var.notification_endpoint}
    {{/is_recovery}}
  EOT

  query = "avg(last_30m):avg:insights.spotify_daily_chart.spotify_api_up{env:${var.environment},service:${var.service_name}} < 0.5"

  monitor_thresholds {
    critical = 0.5
  }

  notify_no_data      = false
  renotify_interval   = 60
  require_full_window = false
  notify_audit        = false
  timeout_h           = 0

  tags = [
    "env:${var.environment}",
    "service:${var.service_name}",
    "application_family:${var.application_family}",
    "team:${var.team_name}",
  ]
}
