/**
 * Copyright 2026 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

variable "context" {
  description = "Context-specific interpolations."
  type = object({
    custom_roles   = optional(map(string), {})
    iam_principals = optional(map(string), {})
    kms_keys       = optional(map(string), {})
    locations      = optional(map(string), {})
    project_ids    = optional(map(string), {})
  })
  default  = {}
  nullable = false
}

variable "kms_key" {
  description = "KMS customer managed encryption key."
  type        = string
  default     = null
}

variable "labels" {
  description = "Labels."
  type        = map(string)
  default     = {}
  nullable    = false
}

variable "message_retention_duration" {
  description = "Minimum duration to retain a message after it is published to the topic."
  type        = string
  default     = null
}

variable "message_storage_enforce_in_transit" {
  description = "If true, var.regions is also used to enforce in-transit guarantees for messages."
  type        = bool
  default     = null
}

variable "name" {
  description = "PubSub topic name."
  type        = string
}

variable "project_id" {
  description = "Project used for resources."
  type        = string
}

variable "regions" {
  description = "List of regions used to set persistence policy."
  type        = list(string)
  default     = []
  nullable    = false
}

variable "schema" {
  description = "Topic schema. If set, all messages in this topic should follow this schema."
  type = object({
    definition   = string
    msg_encoding = optional(string, "ENCODING_UNSPECIFIED")
    schema_type  = string
  })
  default = null
}

variable "subscriptions" {
  description = "Topic subscriptions. Also define push configs for push subscriptions. If options is set to null subscription defaults will be used. Labels default to topic labels if set to null."
  type = map(object({
    ack_deadline_seconds         = optional(number)
    enable_exactly_once_delivery = optional(bool, false)
    enable_message_ordering      = optional(bool, false)
    expiration_policy_ttl        = optional(string)
    filter                       = optional(string)
    iam                          = optional(map(list(string)), {})
    labels                       = optional(map(string))
    message_retention_duration   = optional(string)
    retain_acked_messages        = optional(bool, false)
    bigquery = optional(object({
      table                 = string
      drop_unknown_fields   = optional(bool, false)
      service_account_email = optional(string)
      use_table_schema      = optional(bool, false)
      use_topic_schema      = optional(bool, false)
      write_metadata        = optional(bool, false)
    }))
    cloud_storage = optional(object({
      bucket          = string
      filename_prefix = optional(string)
      filename_suffix = optional(string)
      max_duration    = optional(string)
      max_bytes       = optional(number)
      avro_config = optional(object({
        write_metadata = optional(bool, false)
      }))
    }))
    dead_letter_policy = optional(object({
      topic                 = string
      max_delivery_attempts = optional(number)
    }))
    iam_bindings = optional(map(object({
      members = list(string)
      role    = string
      condition = optional(object({
        expression  = string
        title       = string
        description = optional(string)
      }))
    })), {})
    iam_bindings_additive = optional(map(object({
      member = string
      role   = string
      condition = optional(object({
        expression  = string
        title       = string
        description = optional(string)
      }))
    })), {})
    push = optional(object({
      endpoint   = string
      attributes = optional(map(string))
      no_wrapper = optional(object({
        write_metadata = optional(bool, false)
      }))
      oidc_token = optional(object({
        audience              = optional(string)
        service_account_email = string
      }))
    }))
    retry_policy = optional(object({
      minimum_backoff = optional(number)
      maximum_backoff = optional(number)
    }))
  }))
  default  = {}
  nullable = false
}
