# General variables
variable "environment" {
  description = "Name of the environment, e.g. dev, qa, prod"
  default     = "dev"
}

variable "service_name" {
  description = "Service name, without environment prefix"
  default     = "ows-service"
}

variable "application_family" {
  description = "Application family to which this service belongs"
  type        = string
}

variable "teams" {
  type        = set(string)
  description = "A set of Datadog Teams to associate the resources with"
  default     = []
}

# AWS variables
variable "aws_region" {
  description = "AWS region from which to pull metrics"
  default     = "us-east-1"
}

variable "notification_endpoints" {
  description = "Endpoints for alerts. Can be email, slack, pagerduty, etc"
}

variable "escalation_notification_endpoints" {
  description = "Endpoints for escalation alerts. Can be email, slack, pagerduty, etc"
}

variable "alert_notification_endpoints" {
  type        = string
  description = "Additional endpoints to send only alert notifications to. Can be email, slack, pagerduty, etc."
  default     = ""
}

variable "no_data_notification_endpoints" {
  type        = string
  description = "Additional endpoints to send only no data notifications to. Can be email, slack, pagerduty, etc."
  default     = ""
}

variable "evaluation_delay" {
  description = "Time in seconds to wait for logs to stream in from AWS. Prevents false 'no data'. Only applies to monitors which use standard RDS metrics rather than enhanced metrics, which are realtime."
  default     = 240
}

variable "native_database_integration_enabled" {
  type        = bool
  description = "Whether or not the RDS database has native database metrics available. Note this requires a dedicated Datadog agent service to be configured to collect the metrics, which is not managed by this module. Required for certain monitors and dashboard widgets."
  default     = false
}

variable "cluster_instance_count" {
  type        = number
  description = "The number of instances in the cluster. Used to configure the available DB instance identifier values in the dashboard filter."
  default     = 1
}

variable "rds_engine" {
  type        = string
  description = "The RDS engine type. Must be one of aurora-mysql or aurora-postgresql."
  default     = "aurora-mysql"

  validation {
    condition     = contains(["aurora-mysql", "aurora-postgresql"], var.rds_engine)
    error_message = "rds_engine must be one of aurora-mysql or aurora-postgresql"
  }
}

# Dashboard configuration
variable "replication_widgets_enabled" {
  type        = bool
  description = "Whether or not to enable replication widgets on the dashboard."
  default     = false
}

# Uptime monitor
variable "uptime_monitor_enabled" {
  description = "Enable RDS uptime monitor"
  default     = true
}

variable "measurement_interval_uptime" {
  description = "Measurement interval for the RDS uptime monitor"
  default     = "last_5m"
}

variable "no_data_timeframe_uptime" {
  description = "Time in minutes of receiving no data before uptime monitor is triggered."
  default     = 5
}

# CPU monitor
variable "cpu_monitor_enabled" {
  type        = bool
  description = "Whether or not to enable the CPU monitor."
  default     = true
}

variable "measurement_interval_cpu" {
  description = "Measurement interval for CPU utilization alerts"
  default     = "last_5m"
}

variable "critical_number_cpu" {
  description = "CPU utilization percentage for critical alerts"
  default     = 95
}

variable "critical_recovery_number_cpu" {
  description = "CPU utilization percentage for critical recovery"
  default     = 90
}

variable "warning_number_cpu" {
  description = "CPU utilization percentage for warnings"
  default     = 85
}

variable "warning_recovery_number_cpu" {
  description = "CPU utilization percentage for warning recovery"
  default     = 80
}

variable "ok_number_cpu" {
  description = "CPU utilization percentage that is ok"
  default     = 75
}

# Memory
variable "memory_monitor_enabled" {
  type        = bool
  description = "Whether or not to enable the memory monitor."
  default     = true
}

variable "measurement_interval_memory" {
  description = "Measurement interval for memory utilization alerts"
  default     = "last_5m"
}

variable "critical_number_memory" {
  description = "Memory utilization percentage for critical alerts"
  default     = 98
}

variable "critical_recovery_number_memory" {
  description = "Memory utilization percentage for critical recovery"
  default     = 95
}

variable "warning_number_memory" {
  description = "Memory utilization percentage for warnings"
  default     = 92
}

variable "warning_recovery_number_memory" {
  description = "Memory utilization percentage for warning recovery"
  default     = 90
}

variable "ok_number_memory" {
  description = "Memory utilization percentage that is ok"
  default     = 89
}

# Disk utilization
variable "disk_utilization_monitor_enabled" {
  type        = bool
  description = "Whether or not to enable the disk utilization monitor."
  default     = true
}

variable "measurement_interval_disk_utilization" {
  description = "Measurement interval for disk space utilization percentage alerts"
  default     = "last_5m"
}

variable "critical_number_disk_utilization" {
  description = "Disk space utilization percentage for critical alerts"
  default     = 95
}

variable "critical_recovery_number_disk_utilization" {
  description = "Disk space utilization percentage for critical recovery"
  default     = 90
}

variable "warning_number_disk_utilization" {
  description = "Disk space utilization percentage for warnings"
  default     = 85
}

variable "warning_recovery_number_disk_utilization" {
  description = "Disk space utilization percentage for warning recovery"
  default     = 80
}

variable "ok_number_disk_utilization" {
  description = "Disk space utilization percentage that is ok"
  default     = 75
}

# Replication lag
variable "replication_lag_monitor_enabled" {
  type        = bool
  description = "Whether or not to enable the replication lag monitor. This should only be used for Aurora clusters with multiple instances."
  default     = false
}

variable "measurement_interval_replication_lag" {
  description = "Measurement interval for replication lag alerts"
  default     = "last_5m"
}

variable "critical_number_replication_lag" {
  description = "Replication lag in milliseconds for critical alerts"
  default     = 1800000 # 30 minutes
}

variable "critical_recovery_number_replication_lag" {
  description = "Replication lag in milliseconds for critical recovery"
  default     = 1200000 # 20 minutes
}

variable "warning_number_replication_lag" {
  description = "Replication lag in milliseconds for warnings"
  default     = 900000 # 15 minutes
}

variable "warning_recovery_number_replication_lag" {
  description = "Replication lag in milliseconds for warning recovery"
  default     = 300000 # 5 minutes
}

variable "ok_number_replication_lag" {
  description = "Replication lag in milliseconds that is ok"
  default     = 60000 # 1 minute
}

# Availability
variable "availability_monitor_enabled" {
  type        = bool
  description = "Whether or not to enable the availability monitor."
  default     = false

  validation {
    condition     = !var.availability_monitor_enabled || var.native_database_integration_enabled
    error_message = "native_database_integration_enabled must be set to true to enable the availability monitor."
  }

  validation {
    condition     = !var.availability_monitor_enabled || var.rds_engine == "aurora-mysql"
    error_message = "Availability monitoring is currently only supported for Aurora MySQL databases."
  }
}

variable "critical_number_availability" {
  description = "The number of consecutive connection failures to trigger the availability monitor"
  default     = 1
}

variable "ok_number_availability" {
  description = "The number of consecutive connection successes to resolve the availability monitor"
  default     = 1
}

# Connections
variable "connections_monitor_enabled" {
  type        = bool
  description = "Whether or not to enable the connections monitor."
  default     = false

  validation {
    condition     = !var.connections_monitor_enabled || var.native_database_integration_enabled
    error_message = "native_database_integration_enabled must be set to true to enable the connections monitor."
  }

  validation {
    condition     = !var.connections_monitor_enabled || var.rds_engine == "aurora-mysql"
    error_message = "Connections monitoring is currently only supported for Aurora MySQL databases."
  }
}

variable "measurement_interval_connections" {
  description = "Measurement interval for connection utilization percentage alerts"
  default     = "last_5m"
}

variable "critical_number_connections" {
  description = "Connection utilization percentage for critical alerts"
  default     = 95
}

variable "critical_recovery_number_connections" {
  description = "Connection utilization percentage for critical recovery"
  default     = 90
}

variable "warning_number_connections" {
  description = "Connection utilization percentage for warnings"
  default     = 85
}

variable "warning_recovery_number_connections" {
  description = "Connection utilization percentage for warning recovery"
  default     = 80
}

variable "ok_number_connections" {
  description = "Connection utilization percentage that is ok"
  default     = 75
}

# Slow queries
variable "slow_queries_monitor_enabled" {
  type        = bool
  description = "Whether or not to enable the slow queries monitor."
  default     = false

  validation {
    condition     = !var.slow_queries_monitor_enabled || var.native_database_integration_enabled
    error_message = "native_database_integration_enabled must be set to true to enable the slow queries monitor."
  }

  validation {
    condition     = !var.slow_queries_monitor_enabled || var.rds_engine == "aurora-mysql"
    error_message = "Slow query monitoring is only supported for Aurora MySQL databases."
  }
}

variable "measurement_interval_slow_queries" {
  description = "Measurement interval for slow queries count alerts"
  default     = "last_5m"
}

variable "critical_number_slow_queries" {
  description = "Slow query count for critical alerts"
  default     = 25
}

variable "critical_recovery_number_slow_queries" {
  description = "Slow query count for critical recovery"
  default     = 20
}

variable "warning_number_slow_queries" {
  description = "Slow query count for warnings"
  default     = 15
}

variable "warning_recovery_number_slow_queries" {
  description = "Slow query count for warning recovery"
  default     = 10
}

variable "ok_number_slow_queries" {
  description = "Slow query count that is ok"
  default     = 5
}

# Event Notifications
variable "event_notification_monitor_enabled" {
  type        = bool
  description = "Whether or not to enable the event notification monitor."
  default     = true
}

variable "event_notification_monitor_event_types" {
  type        = list(string)
  description = "List of event types to monitor. Glob matching is accepted."
  default     = [
     "availability",
     "*failover", # matches both failover and "global failover" events
     "failure",
     "deletion",
     "*storage", # also matches "low storage"
     "recovery",
     "restoration"
  ]
}

variable "measurement_interval_events" {
  description = "Measurement interval for event count alerts"
  default     = "15m"
}

variable "critical_number_events" {
  description = "Event count for critical alerts"
  default     = 1
}

variable "warning_number_events" {
  description = "Event count for warnings"
  default     = 0.5
}

variable "ok_number_events" {
  description = "Event count that is ok"
  default     = 0
}

variable "dependent_applications" {
  type        = set(string)
  description = "Optional list of dependent applications to set as a dependent_application tag on resources. These will be combined with programmatically set required tags."
  default     = []
}

locals {
  # Only team tags are supported for dashboards
  dashboard_tags = [for team in var.teams : "team:${team}"]

  tags = flatten([
    "application_family:${var.application_family}",
    "env:${var.environment}",
    "environment:${var.environment}",
    "service:${var.service_name}",
    "service_name:${var.service_name}",
    [for team in var.teams : "team:${team}"],
    [for dependent_application in var.dependent_applications : "dependent_application:${dependent_application}"]
  ])
}

variable "excluded_identifiers" {
  description = "List of patterns to exclude from the metric query."
  type        = list(string)
  default     = ["*-green-*", "*-old*", "*-tmp-*", "*read-only*"]
}
