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

variable "service_name" {
  type        = string
  description = "Service name, without environment prefix"
}

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

variable "bucket_name" {
  type        = string
  description = "Name of the S3 bucket, if not of the format <environment>-<service_name>"
  default     = null
}

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

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

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

variable "dashboard_configuration" {
  type = object({
    enabled                       = bool
    show_advanced_storage_metrics = optional(bool, false) # Requires S3 storage lens to be enabled in terraform-s3
    show_replication_metrics      = optional(bool, false) # Requires replication metrics to be enabled in terraform-s3, and also storage lens for some widgets
    show_activity_metrics         = optional(bool, false) # Requires storage lens to be enabled in terraform-s3 with activity metrics enabled
  })
  default = {
    enabled = true
  }
}

variable "replication_failures_monitor_configuration" {
  type = object({
    enabled                     = bool
    critical_threshold          = optional(number, 0)
    critical_recovery_threshold = optional(number)
    warning_threshold           = optional(number)
    warning_recovery_threshold  = optional(number)
  })
  description = "Configuration for replication failures monitor. The bucket must have at least one replication rule with metrics enabled. Default settings will notify on a single failure."
  default = {
    enabled = false
  }
}

variable "replication_latency_monitor_configuration" {
  type = object({
    enabled                     = bool
    critical_threshold          = optional(number, 2 * 60 * 60)
    critical_recovery_threshold = optional(number)
    warning_threshold           = optional(number, 60 * 60)
    warning_recovery_threshold  = optional(number)
  })
  description = "Configuration for replication latency monitor. The bucket must have at least one replication rule with metrics enabled."
  default = {
    enabled = false
  }
}

variable "guardduty_malware_monitor_configuration" {
  type = object({
    enabled = bool
  })
  description = "Configuration for GuardDuty S3 Malware monitor."
  default = {
    enabled = false
  }
}

locals {
  bucket_name = coalesce(var.bucket_name, "${var.environment}-${var.service_name}")

  # 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}"],
  ])
}
