locals {
  contract_lifecycle_automation_lambda_name = "${var.service_name}-contract-lifecycle-automation"
}

data "aws_secretsmanager_secret" "datadog_api" {
  name = "${var.environment}/datadog/DD_API_KEY"
}

data "aws_secretsmanager_secret_version" "datadog_api" {
  secret_id = data.aws_secretsmanager_secret.datadog_api.id
}

data "aws_secretsmanager_secret" "datadog_app_key" {
  name = "${var.environment}/datadog/DD_APP_KEY"
}

data "aws_secretsmanager_secret_version" "datadog_app_key" {
  secret_id = data.aws_secretsmanager_secret.datadog_app_key.id
}

# Datadog provider
provider "datadog" {
  api_key = data.aws_secretsmanager_secret_version.datadog_api.secret_string
  app_key = data.aws_secretsmanager_secret_version.datadog_app_key.secret_string
}

resource "datadog_monitor" "kafka_event_nr_contribution_metadata_offset_lag_monitor" {
  count   = 1 # set to 0 to disable the monitor
  name    = "${var.environment}-${var.service_name}-schedule-auto-add-consumer-offset-lag"
  type    = "metric alert"
  message = "Consumer offset lag is to high for the last 24h. Notify: ${var.escalation_notification_endpoints} ${var.on_call_endpoint}"

  query = "avg(last_10m):avg:aws.kafka.sum_offset_lag{environment:${var.environment},topic:event.nr.contribution.metadata} by {topic} > ${var.datadog_error_monitor_threshold}"

  monitor_thresholds {
    critical          = var.datadog_error_monitor_threshold
    warning           = var.datadog_warning_monitor_threshold
    critical_recovery = var.datadog_recovery_monitor_threshold
  }

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

  tags = [
    "environment:${var.environment}",
    "service_name:${var.service_name}-schedule-auto-add",
    "application_family:${var.application_family}"
  ]
}


resource "datadog_monitor" "contract_lifecycle_automation_invocation_monitor" {
  count   = 1 # set to 0 to disable the monitor
  name    = "${var.environment}-${local.contract_lifecycle_automation_lambda_name}-invocation-monitor"
  type    = "metric alert"
  message = "The contract lifecycle automation lambda did not trigger at 12AM UTC. Notify: ${var.notification_endpoints}"

  query = "sum(last_1500m):default(sum:aws.lambda.invocations{environment:${var.environment},service_name:${local.contract_lifecycle_automation_lambda_name}}.as_count().rollup(sum, daily, 12am), 0) <= ${var.datadog_invocation_error_monitor_threshold}"

  monitor_thresholds {
    critical          = var.datadog_invocation_error_monitor_threshold
    critical_recovery = var.datadog_invocation_recovery_monitor_threshold
  }

  notify_no_data      = false
  notify_audit        = false
  renotify_interval   = 720
  timeout_h           = 0
  require_full_window = true
  evaluation_delay    = 900

  tags = [
    "environment:${var.environment}",
    "service_name:${local.contract_lifecycle_automation_lambda_name}",
    "application_family:${var.application_family}",
    "team:abacus"
  ]
}

resource "datadog_monitor" "kafka_event_accounts_offset_lag_monitor" {
  count   = 1 # set to 0 to disable the monitor
  name    = "${var.environment}-${var.service_name}-sync-account-offset-lag"
  type    = "metric alert"
  message = "{{#is_alert}}\n Critical Alert: The consumer offset lag is over ${var.datadog_sync_account_error_monitor_threshold} for the last 10min. Notify: ${var.notification_endpoints} ${var.on_call_endpoint} \n{{/is_alert}} \n\n{{#is_warning}}\n Warning: The consumer offset lag is over ${var.datadog_sync_account_warning_monitor_threshold} for the last 10min. Notify: ${var.notification_endpoints} \n{{/is_warning}}"

  query = "avg(last_10m):avg:aws.kafka.sum_offset_lag{environment:${var.environment},topic:${var.sync_account_kafka_topic}} by {topic} >= ${var.datadog_sync_account_error_monitor_threshold}"

  monitor_thresholds {
    critical          = var.datadog_sync_account_error_monitor_threshold
    warning           = var.datadog_sync_account_warning_monitor_threshold
    critical_recovery = var.datadog_sync_account_recovery_monitor_threshold
  }

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

  tags = [
    "environment:${var.environment}",
    "service_name:${var.service_name}-sync-account",
    "application_family:${var.application_family}",
    "team:abacus"
  ]
}

# ==============================================================================
# Accounting Run Pipeline - Error & Duration Monitors
# ==============================================================================

# --- lambda-abacus-ledger-accounting-run-balance ---

resource "datadog_monitor" "ledger_accounting_run_balance_error_monitor" {
  count   = 1 # set to 0 to disable the monitor
  name    = "${var.environment}-${var.service_name}-ledger-accounting-run-balance-error-monitor"
  type    = "metric alert"
  message = "{{#is_alert}}\n Critical Alert: The ${var.service_name}-ledger-accounting-run-balance lambda has errors in the last 5 minutes. Notify: ${var.notification_endpoints}\n{{/is_alert}}"

  query = "sum(last_5m):sum:aws.lambda.errors{environment:${var.environment},service_name:${var.service_name}-ledger-accounting-run-balance}.as_count() > 0"

  monitor_thresholds {
    critical = 0
  }

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

  tags = [
    "environment:${var.environment}",
    "service_name:${var.service_name}-ledger-accounting-run-balance",
    "application_family:${var.application_family}",
    "team:abacus"
  ]
}

resource "datadog_monitor" "ledger_accounting_run_balance_duration_monitor" {
  count   = 1 # set to 0 to disable the monitor
  name    = "${var.environment}-${var.service_name}-ledger-accounting-run-balance-duration-monitor"
  type    = "metric alert"
  message = "{{#is_alert}}\n Critical Alert: The ${var.service_name}-ledger-accounting-run-balance lambda average duration exceeded ${var.ledger_accounting_run_balance_duration_threshold}ms in the last 5 minutes. Notify: ${var.notification_endpoints}\n{{/is_alert}}\n{{#is_warning}}\n Warning: The ${var.service_name}-ledger-accounting-run-balance lambda duration is elevated. Notify: ${var.notification_endpoints}\n{{/is_warning}}"

  query = "avg(last_5m):avg:aws.lambda.duration{environment:${var.environment},service_name:${var.service_name}-ledger-accounting-run-balance} > ${var.ledger_accounting_run_balance_duration_threshold}"

  monitor_thresholds {
    critical = var.ledger_accounting_run_balance_duration_threshold
    warning  = var.ledger_accounting_run_balance_duration_warning_threshold
  }

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

  tags = [
    "environment:${var.environment}",
    "service_name:${var.service_name}-ledger-accounting-run-balance",
    "application_family:${var.application_family}",
    "team:abacus"
  ]
}

# --- lambda-abacus-commit-royalties ---

resource "datadog_monitor" "commit_royalties_error_monitor" {
  count   = 1 # set to 0 to disable the monitor
  name    = "${var.environment}-${var.service_name}-commit-royalties-error-monitor"
  type    = "metric alert"
  message = "{{#is_alert}}\n Critical Alert: The ${var.service_name}-commit-royalties lambda has errors in the last 5 minutes. Notify: ${var.notification_endpoints}\n{{/is_alert}}"

  query = "sum(last_5m):sum:aws.lambda.errors{environment:${var.environment},service_name:${var.service_name}-commit-royalties}.as_count() > 0"

  monitor_thresholds {
    critical = 0
  }

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

  tags = [
    "environment:${var.environment}",
    "service_name:${var.service_name}-commit-royalties",
    "application_family:${var.application_family}",
    "team:abacus"
  ]
}

resource "datadog_monitor" "commit_royalties_duration_monitor" {
  count   = 1 # set to 0 to disable the monitor
  name    = "${var.environment}-${var.service_name}-commit-royalties-duration-monitor"
  type    = "metric alert"
  message = "{{#is_alert}}\n Critical Alert: The ${var.service_name}-commit-royalties lambda average duration exceeded ${var.commit_royalties_duration_threshold}ms in the last 5 minutes. Notify: ${var.notification_endpoints}\n{{/is_alert}}\n{{#is_warning}}\n Warning: The ${var.service_name}-commit-royalties lambda duration is elevated. Notify: ${var.notification_endpoints}\n{{/is_warning}}"

  query = "avg(last_5m):avg:aws.lambda.duration{environment:${var.environment},service_name:${var.service_name}-commit-royalties} > ${var.commit_royalties_duration_threshold}"

  monitor_thresholds {
    critical = var.commit_royalties_duration_threshold
    warning  = var.commit_royalties_duration_warning_threshold
  }

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

  tags = [
    "environment:${var.environment}",
    "service_name:${var.service_name}-commit-royalties",
    "application_family:${var.application_family}",
    "team:abacus"
  ]
}

# --- lambda-abacus-commit-mechanicals ---
#
# No error monitor defined here: lambda_abacus_commit_mechanicals_datadog_monitor
# (commit-mechanicals.tf) already provides prod-lambda-abacus-commit-mechanicals-error-monitor
# via lambda_error_monitor_enabled = true.

resource "datadog_monitor" "commit_mechanicals_duration_monitor" {
  count   = 1 # set to 0 to disable the monitor
  name    = "${var.environment}-${var.service_name}-commit-mechanicals-duration-monitor"
  type    = "metric alert"
  message = "{{#is_alert}}\n Critical Alert: The ${var.service_name}-commit-mechanicals lambda average duration exceeded ${var.commit_mechanicals_duration_threshold}ms in the last 5 minutes. Notify: ${var.notification_endpoints}\n{{/is_alert}}\n{{#is_warning}}\n Warning: The ${var.service_name}-commit-mechanicals lambda duration is elevated. Notify: ${var.notification_endpoints}\n{{/is_warning}}"

  query = "avg(last_5m):avg:aws.lambda.duration{environment:${var.environment},service_name:${var.service_name}-commit-mechanicals} > ${var.commit_mechanicals_duration_threshold}"

  monitor_thresholds {
    critical = var.commit_mechanicals_duration_threshold
    warning  = var.commit_mechanicals_duration_warning_threshold
  }

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

  tags = [
    "environment:${var.environment}",
    "service_name:${var.service_name}-commit-mechanicals",
    "application_family:${var.application_family}",
    "team:abacus"
  ]
}

# --- lambda-abacus-reserves-take ---

resource "datadog_monitor" "reserves_take_error_monitor" {
  count   = 1 # set to 0 to disable the monitor
  name    = "${var.environment}-${var.service_name}-reserves-take-error-monitor"
  type    = "metric alert"
  message = "{{#is_alert}}\n Critical Alert: The ${var.service_name}-reserves-take lambda has errors in the last 5 minutes. Notify: ${var.notification_endpoints}\n{{/is_alert}}"

  query = "sum(last_5m):sum:aws.lambda.errors{environment:${var.environment},service_name:${var.service_name}-reserves-take}.as_count() > 0"

  monitor_thresholds {
    critical = 0
  }

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

  tags = [
    "environment:${var.environment}",
    "service_name:${var.service_name}-reserves-take",
    "application_family:${var.application_family}",
    "team:abacus"
  ]
}

resource "datadog_monitor" "reserves_take_duration_monitor" {
  count   = 1 # set to 0 to disable the monitor
  name    = "${var.environment}-${var.service_name}-reserves-take-duration-monitor"
  type    = "metric alert"
  message = "{{#is_alert}}\n Critical Alert: The ${var.service_name}-reserves-take lambda average duration exceeded ${var.reserves_take_duration_threshold}ms in the last 5 minutes. Notify: ${var.notification_endpoints}\n{{/is_alert}}\n{{#is_warning}}\n Warning: The ${var.service_name}-reserves-take lambda duration is elevated. Notify: ${var.notification_endpoints}\n{{/is_warning}}"

  query = "avg(last_5m):avg:aws.lambda.duration{environment:${var.environment},service_name:${var.service_name}-reserves-take} > ${var.reserves_take_duration_threshold}"

  monitor_thresholds {
    critical = var.reserves_take_duration_threshold
    warning  = var.reserves_take_duration_warning_threshold
  }

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

  tags = [
    "environment:${var.environment}",
    "service_name:${var.service_name}-reserves-take",
    "application_family:${var.application_family}",
    "team:abacus"
  ]
}

# --- lambda-abacus-reserves-schedule ---

resource "datadog_monitor" "reserves_schedule_error_monitor" {
  count   = 1 # set to 0 to disable the monitor
  name    = "${var.environment}-${var.service_name}-reserves-schedule-error-monitor"
  type    = "metric alert"
  message = "{{#is_alert}}\n Critical Alert: The ${var.service_name}-reserves-schedule lambda has errors in the last 5 minutes. Notify: ${var.notification_endpoints}\n{{/is_alert}}"

  query = "sum(last_5m):sum:aws.lambda.errors{environment:${var.environment},service_name:${var.service_name}-reserves-schedule}.as_count() > 0"

  monitor_thresholds {
    critical = 0
  }

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

  tags = [
    "environment:${var.environment}",
    "service_name:${var.service_name}-reserves-schedule",
    "application_family:${var.application_family}",
    "team:abacus"
  ]
}

resource "datadog_monitor" "reserves_schedule_duration_monitor" {
  count   = 1 # set to 0 to disable the monitor
  name    = "${var.environment}-${var.service_name}-reserves-schedule-duration-monitor"
  type    = "metric alert"
  message = "{{#is_alert}}\n Critical Alert: The ${var.service_name}-reserves-schedule lambda average duration exceeded ${var.reserves_schedule_duration_threshold}ms in the last 5 minutes. Notify: ${var.notification_endpoints}\n{{/is_alert}}\n{{#is_warning}}\n Warning: The ${var.service_name}-reserves-schedule lambda duration is elevated. Notify: ${var.notification_endpoints}\n{{/is_warning}}"

  query = "avg(last_5m):avg:aws.lambda.duration{environment:${var.environment},service_name:${var.service_name}-reserves-schedule} > ${var.reserves_schedule_duration_threshold}"

  monitor_thresholds {
    critical = var.reserves_schedule_duration_threshold
    warning  = var.reserves_schedule_duration_warning_threshold
  }

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

  tags = [
    "environment:${var.environment}",
    "service_name:${var.service_name}-reserves-schedule",
    "application_family:${var.application_family}",
    "team:abacus"
  ]
}

# ==============================================================================
# Adjustments Apply - Error Monitor (timeout-filtered)
# ==============================================================================
#
# The module's built-in error monitor (lambda_error_monitor_enabled = false in
# main.tf) is replaced by this log alert, which filters out ReadTimeout errors.
# Since user retries were introduced, timeouts are no longer an outage — only
# non-timeout errors should page. The module still manages the dashboard and
# invocation monitor.

resource "datadog_monitor" "adjustments_apply_error_monitor" {
  count   = 1 # set to 0 to disable the monitor
  name    = "${var.environment}-${local.lambda_abacus_adjustments_apply_service_name}-error-monitor"
  type    = "log alert"
  message = "{{#is_alert}}\n Critical Alert: Non-timeout error detected in ${local.lambda_abacus_adjustments_apply_service_name}. Notify: ${var.notification_endpoints} ${var.escalation_notification_endpoints} ${var.on_call_endpoint}\n{{/is_alert}}"

  query = "logs(\"service:${local.lambda_abacus_adjustments_apply_service_name} status:error -ReadTimeout -\\\"The read operation timed out\\\"\").index(\"*\").rollup(\"count\").last(\"15m\") > 0"

  monitor_thresholds {
    critical = 0
  }

  enable_logs_sample  = true
  notify_no_data      = false
  notify_audit        = false
  renotify_interval   = 60
  timeout_h           = 0
  require_full_window = false
  evaluation_delay    = 300

  tags = [
    "environment:${var.environment}",
    "service_name:${local.lambda_abacus_adjustments_apply_service_name}",
    "application_family:${var.application_family}",
    "team:abacus"
  ]
}
