# Variables ====================================================================

variable "enable" {
  description = "Enable or suspend the GuardDuty detector"
  type        = bool
  default     = true
}

variable "finding_publishing_frequency" {
  description = "Frequency of findings export — FIFTEEN_MINUTES, ONE_HOUR, or SIX_HOURS"
  type        = string
  default     = "FIFTEEN_MINUTES"

  validation {
    condition     = contains(["FIFTEEN_MINUTES", "ONE_HOUR", "SIX_HOURS"], var.finding_publishing_frequency)
    error_message = "finding_publishing_frequency must be FIFTEEN_MINUTES, ONE_HOUR, or SIX_HOURS."
  }
}

variable "detector_features" {
  description = "Detector protection features. Key = feature name, status = ENABLED or DISABLED. RUNTIME_MONITORING and EKS_RUNTIME_MONITORING support additional_configuration."
  type = map(object({
    status = string
    additional_configuration = optional(map(object({
      status = string
    })), {})
  }))

  validation {
    condition     = alltrue([for f in values(var.detector_features) : contains(["ENABLED", "DISABLED"], f.status)])
    error_message = "detector_features status must be ENABLED or DISABLED."
  }

  validation {
    condition = alltrue([
      for f in values(var.detector_features) :
      alltrue([
        for ac in values(f.additional_configuration) :
        contains(["ENABLED", "DISABLED"], ac.status)
      ])
    ])
    error_message = "additional_configuration status must be ENABLED or DISABLED."
  }
}

variable "detector_organization_configuration" {
  description = "Organization-wide GuardDuty settings. Set to null to skip. auto_enable_org_members = ALL, NEW, or NONE."
  type = object({
    auto_enable_org_members = string
    features = map(object({
      auto_enable = string
      additional_configuration = optional(map(object({
        auto_enable = string
      })), {})
    }))
  })
  nullable = true
  default  = null

  validation {
    condition = var.detector_organization_configuration == null || contains(
      ["ALL", "NEW", "NONE"],
      var.detector_organization_configuration.auto_enable_org_members
    )
    error_message = "auto_enable_org_members must be ALL, NEW, or NONE."
  }

  validation {
    condition = var.detector_organization_configuration == null || alltrue([
      for f in values(var.detector_organization_configuration.features) :
      contains(["ALL", "NEW", "NONE"], f.auto_enable)
    ])
    error_message = "features auto_enable must be ALL, NEW, or NONE."
  }

  validation {
    condition = var.detector_organization_configuration == null || alltrue([
      for f in values(var.detector_organization_configuration.features) :
      alltrue([
        for ac in values(f.additional_configuration) :
        contains(["ALL", "NEW", "NONE"], ac.auto_enable)
      ])
    ])
    error_message = "additional_configuration auto_enable must be ALL, NEW, or NONE."
  }
}

variable "tags" {
  description = "Resource tags to apply to all resources"
  type        = map(string)
  default     = {}
}
