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

variable "service_name" {
  description = "Service name, without environment prefix"
  type        = string
  default     = "ows-service"
}

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

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

variable "vpc_id" {
  description = "VPC in which to run the task"
  type        = string
  default     = "vpc-34dbfd51"
}

variable "additional_cidr_blocks_enabled" {
  description = "Whether or not to allow additional CIDR blocks. Used for access to elasticache cluster from outside VPC."
  type        = string
  default     = false
}

variable "additional_cidr_blocks" {
  description = "VPN CIDR blocks from which to allow additional access"
  type        = list(string)
  default = [
    "10.30.0.0/22",
    "192.168.31.0/24",
    "192.168.32.0/24",
    "192.168.40.0/22",
  ]
}

variable "additional_source_security_group_ids" {
  description = "Security Group IDs from which to allow traffic to cache cluster"
  type        = list(string)
  default     = []
}

variable "auto_minor_version_upgrade_enabled" {
  description = "Whether or not to automatically upgrade minor versions"
  type        = bool
  default     = true
}

variable "cache_engine" {
  description = "Engine for cache: specify redis or memcached"
  type        = string
  default     = "redis"
}

variable "cache_node_count" {
  description = "Number of cache instances in cluster"
  type        = number
  default     = 2
}

variable "cache_maintenance_window" {
  description = "Weekly window for maintenance tasks. Minimum 60 minutes"
  type        = string
  default     = "sun:05:00-sun:09:00"
}

variable "cache_node_type" {
  description = "Type of the cache node"
  type        = string
  default     = "cache.t4g.micro"
}

variable "cache_parameter_group_name" {
  description = "Parameter group name. If not specified, default group will be used"
  type        = string
  default     = ""
}

variable "cache_at_rest_encryption_enabled" {
  description = "Enable encryption at rest. Changing this value on an existing ElastiCache instance will re-create it, which will cause an outage."
  type        = string
  default     = "true"
}

variable "cache_transit_encryption_enabled" {
  description = "Enable encryption in transit. Changing this value on an existing ElastiCache instance will re-create it, which will cause an outage."
  type        = string
  default     = "true"
}

variable "cache_auth_token" {
  description = "Configure password-protected access. Use with caution, as the value specified here will be available in plain text in the terraform state file. Requires encryption in transit to be enabled."
  type        = string
  default     = null
}

variable "cache_subnet_group_name" {
  description = "Name of the cache subnet group"
  type        = string
  default     = "dev-elasticache-group"
}

# Redis settings
variable "redis_engine_version" {
  description = "Redis engine version"
  type        = string
  default     = "7.1"
}

variable "redis_snapshot_window" {
  description = "Time window for daily backups"
  type        = string
  default     = "03:00-05:00"
}

variable "redis_snapshot_retention_limit" {
  description = "Number of days to retain backups"
  type        = number
  default     = 7
}

# Memcached settings
variable "memcached_engine_version" {
  description = "Memcached engine version"
  type        = string
  default     = "1.6.22"
}

locals {
  cache_parameter_default_group = {
    memcached = "default.memcached1.6"
    redis     = "default.redis7"
  }

  primsa_sg_exception_tags = {
    "eiso-exception" = "aws.08.30"
  }

  combined_resource_tags = merge(
    {
      environment        = var.environment
      service_name       = var.service_name
      application_family = var.application_family
      terraformed        = true
    },
    var.additional_tags
  )

  graviton_instance = can(regex("^cache\\.[^.]*g[^.]*\\..*$", var.cache_node_type))
  valid_cache_type = (var.environment == "prod" && local.graviton_instance) || (var.environment != "prod" && can(index([
    "cache.t4g.micro", "cache.t4g.small"
  ], var.cache_node_type)))

  route53_zone_id = var.override_route53_zone_id != null ? var.override_route53_zone_id : (var.environment == "dev" ? "Z21XEY26C989RH" : "Z0183645HDT0XCWHLW7S")
}

variable "route53_record_creation_enabled" {
  description = "Whether or not to create Route 53 DNS records"
  type        = bool
  default     = true
}

variable "route53_ttl" {
  description = "TTL for Route 53 records"
  type        = string
  default     = "60"
}

# Not real output - only used to validate and enforce graviton instance type and size.
output "validate_instance" {
  value = null

  precondition {
    condition     = local.valid_cache_type
    error_message = "Graviton-based instance types are recommended for this cluster. Please select cache.t4g.micro or cache.t4g.small for non-production workloads."
  }
}

variable "override_route53_zone_id" {
  description = "AWS Route 53 zone id to override the default zone ID"
  type        = string
  default     = null
}

variable "vpn_prefix_list_names" {
  description = "List of VPN prefix list names to allow access."
  type        = list(string)
  default     = [
    "vpn-ny-users"
  ]
}
