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

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

variable "cluster_name" {
  description = "Service name, without environment prefix"
  default     = "managed-kafka-cdc-destination"
}

# Slack Notifications
variable "notification_endpoints" {
  description = "A comma-separated list of notification endpoints (emails, Slack channels, etc.)"
  default     = "@slack-kafka-data-highway-alerts"
}

variable "escalation_notification_endpoints" {
  description = "A comma-separated list of escalation notification endpoints (emails, Slack channels, etc.)"
  default     = "@slack-kafka-data-highway"
}


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

# MSK variables
variable "broker_instance_type" {
  description = "The MSK broker instance type (e.g., kafka.m5.large, kafka.m7g.xlarge)"
  type        = string
  default     = "kafka.m5.large"
}

variable "topic_categories" {
  description = "Map of topic categories to regex patterns for topic usage tracking"
  type = map(object({
    name    = string
    pattern = string
  }))
  default = {
    cdc = {
      name    = "CDC"
      pattern = "cdc.*"
    }
    stream = {
      name    = "Stream"
      pattern = "stream."
    }
    event = {
      name    = "Event"
      pattern = "event.*"
    }
    dlq = {
      name    = "DLQs"
      pattern = "dlq.*"
    }
    kafka_connect = {
      name    = "Kafka Connect"
      pattern = "_kafka_connect*"
    }
  }
}

variable "msk_cpu_monitor_enabled" {
  description = "Whether to enable the MSK CPU monitor"
  type        = bool
  default     = true
}

variable "msk_cpu_monitor_critical_threshold" {
  description = "Critical threshold for the MSK CPU monitor (%)"
  type        = number
  default     = 90
}

variable "msk_cpu_monitor_warning_threshold" {
  description = "Warning threshold for the MSK CPU monitor (%)"
  type        = number
  default     = 75
}

variable "msk_memory_monitor_critical_threshold" {
  description = "Critical threshold for the MSK Memory monitor (%)"
  type        = number
  default     = 75
}

variable "msk_memory_monitor_warning_threshold" {
  description = "Warning threshold for the MSK Memory monitor (%)"
  type        = number
  default     = 60
}

variable "msk_memory_monitor_enabled" {
  description = "Whether to enable the MSK Memory monitor"
  type        = bool
  default     = true
}

variable "msk_partition_limit_monitor_enabled" {
  description = "Whether to enable the MSK Partition Limit monitor"
  type        = bool
  default     = true
}


locals {
  # Cluster filter for all queries
  cluster_filter = "cluster_name:${var.environment}-${var.cluster_name}"

  # Determine broker partition limit based on instance type size
  broker_partition_limit = (
    endswith(var.broker_instance_type, ".small") ? 300 :
    endswith(var.broker_instance_type, ".large") ? 1500 :
    endswith(var.broker_instance_type, ".xlarge") ? 1500 :
    endswith(var.broker_instance_type, ".2xlarge") ? 3000 :
    endswith(var.broker_instance_type, ".xlarge") ? 1500 :
    6000
  )

  timeseries_definitions = [
    {
      title = "Active Clients"
      query = "avg:aws.kafka.client_connection_count{${local.cluster_filter}}"

      show_legend = false
      legend_size = "0"

      yaxis_scale = "linear"

      legend_columns = ["avg", "max", "min"]

      pos_x  = 0
      pos_y  = 5
      width  = 12
      height = 5
    },
    {
      title = "Bytes IN per Second"
      query = "avg:aws.kafka.bytes_in_per_sec{${local.cluster_filter},!topic:_*} by {topic}"

      show_legend = true
      legend_size = "16"

      yaxis_scale = "log"

      legend_columns = ["avg", "max", "min"]

      pos_x  = 0
      pos_y  = 5
      width  = 12
      height = 7
    },
    {
      title = "Bytes OUT per Second"
      query = "avg:aws.kafka.bytes_out_per_sec{${local.cluster_filter},!topic:_*} by {topic}"

      show_legend = true
      legend_size = "16"

      yaxis_scale = "log"

      legend_columns = ["avg", "max", "min"]

      pos_x  = 0
      pos_y  = 12
      width  = 12
      height = 7
    },
    {
      title = "Topic Offset Lag"
      query = "avg:aws.kafka.max_offset_lag{${local.cluster_filter},!topic:_*} by {topic}"

      show_legend = true
      legend_size = "16"

      yaxis_scale = "log"

      legend_columns = ["avg", "max", "min"]

      pos_x  = 0
      pos_y  = 19
      width  = 12
      height = 7
    },
  ]

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