variable "env" {
  description = "Available values: dev, qa, prod."
}

variable "table_name" {
  description = "Name of dynamodb table"
}

# this should be the reasonable default for all new tables
variable "use_on_demand" {
  description = "Whether to use On-Demand mode"
  default     = true
}

variable "read_capacity" {
  description = "Read capacity"
  default     = 1
}

variable "write_capacity" {
  description = "Write capacity"
  default     = 1
}

variable "hash_key" {
  description = "Name of the primary key"
  default     = "primary_key"
}

variable "range_key" {
  description = "Name of the sort key"
  default     = "sort_key"
}

variable "range_type" {
  description = "Sort key type: 'S' for String, 'B' for Boolean, 'N' for Numeric"
  default     = "S"
}

variable "range_key_enabled" {
  description = "Add a sort key? true or false"
  default     = false
}

variable "view_type" {
  description = "Stream view type"
  default     = "NEW_AND_OLD_IMAGES"
}

variable "max_read_capacity" {
  description = "maximum read provisioned capacity"
  default     = 5
}

variable "min_read_capacity" {
  description = "minimum read provisioned capacity"
  default     = 1
}

variable "read_capacity_utilization_percentage" {
  description = "percentage threshold to use to scale read capacity"
  default     = 70
}

variable "max_write_capacity" {
  description = "maximum write provisioned capacity"
  default     = 5
}

variable "min_write_capacity" {
  description = "minimum write provisioned capacity"
  default     = 1
}

variable "write_capacity_utilization_percentage" {
  description = "percentage threshold to use to scale write capacity"
  default     = 70
}

variable "use_custom_table_name" {
  description = "Whether to use a custom table name; if so specify a name in var.custom_table_name"
  default     = false
}

variable "custom_table_name" {
  description = "User-provided table name if var.use_custom_table_name is set to true"
  default     = "my-custom-table-name"
}

variable "server_side_encryption_enabled" {
  description = "Whether or not to enable encryption at rest using an AWS managed KMS customer master key (CMK). If enabled is false then server-side encryption is set to AWS-owned key (shown as DEFAULT in the AWS console). Potentially confusingly, if enabled is true and no kms_key_arn is specified then server-side encryption is set to the default KMS-managed key (shown as KMS in the AWS console). The AWS KMS documentation explains the difference between AWS-owned and KMS-managed keys."
  default     = true
}

variable "kms_key_arn" {
  description = "ARN of the CMK that should be used for the AWS KMS encryption. This argument should only be used if the key is different from the default KMS-managed DynamoDB key, `alias/aws/dynamodb`. Note: This attribute will not be populated with the ARN of default keys."
  default     = null
}

variable "point_in_time_recovery_enabled" {
  default = true
}

variable "attribute" {
  type = list(object({
    name = string
    type = string
    }
  ))
  description = "List of additional attributes"
}

variable "global_secondary_index" {
  type = list(object({
    name            = string
    hash_key        = string
    range_key       = optional(string)
    projection_type = string
    write_capacity  = optional(number)
    read_capacity   = optional(number)
    }
  ))
  description = "List of GSIs for the table"
  default     = []
}

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

variable "additional_tags" {
  type        = map(string)
  description = "Optional map of additional tags to set on resources. These will be combined with programmatically set required tags."
  default     = {}
}

variable "ttl_enabled" {
  default = false
}

variable "ttl_attribute_name" {
  default = ""
}

# enabling this has severe limitations/gotchas
# 1. enabling works seamlessly only with PAY_PER_REQUEST
# 2. table has to be empty when enabling
# 3. tableMaxWriteCapacityUnits must match in every region the table is to be created for Pay per request
# 4. table and index WCU must match in all regions for provisioned
# 5. table and index WCU autoscaling must be enabled for provisioned
#    - this trips up terraform provider and means that a workaround needs to be done
#    - workaround: create table without replicas with autoscaling, then enable replicas in a second apply
# 6. table streaming must be enabled with OLD AND NEW image
variable "global_table_replica_regions" {
  description = "list of regions where global table should be in addition to main region"
  type        = list(string)
  default     = []
}

# Helper locals for programmatic generation. These should not be changed.
locals {
  # Prerender dynamodb table name
  generated_table_name = "${var.env}_${var.table_name}"

  combined_resource_tags = merge(
    {
      Name               = var.use_custom_table_name ? var.custom_table_name : local.generated_table_name
      environment        = var.env
      service_name       = var.table_name
      application_family = var.application_family
      terraformed        = true
    },
    var.additional_tags
  )
}
