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

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

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

variable "aws_region" {
  description = "AWS region"
  default     = "us-east-1"
}

variable "vpc_id" {
  type        = string
  description = "The ID of the VPC for the RDS security group."
}

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 "rds_refresh_tags" {
  type        = map(string)
  description = "Map of additional tags to set on kms key. Required by RDS refresh."
  default = {
    allow_rds_refresh = true
  }
}

# RDS event subscription variables
variable "rds_cluster_event_notification_categories" {
  description = "Categories of RDS cluster events that trigger notifications. This normally should not be changed."
  type        = list(string)

  default = [
    "failover",
    "failure",
    "notification",
  ]
}

variable "rds_instance_event_notification_categories" {
  description = "Categories of RDS instance events that trigger notifications. This normally should not be changed."
  type        = list(string)

  default = [
    "availability",
    "backup",
    "configuration change",
    "creation",
    "deletion",
    "failover",
    "failure",
    "low storage",
    "maintenance",
    "notification",
    "read replica",
    "recovery",
    "restoration",
  ]
}

variable "rds_event_notification_enabled" {
  description = "Whether or not RDS notifications are enabled. This normally should not be changed."
  default     = true
}

# RDS cluster variables
variable "rds_primary_cluster_enabled" {
  description = "Whether or not to create a primary RDS cluster."
  default     = true
}

variable "rds_serverless_cluster_enabled" {
  description = "Whether the RDS cluster should run the serverless engine. If true, set var.rds_primary_cluster_enabled to false and rds_cluster_instance_count to 0"
  default     = false
}

variable "rds_allow_major_version_upgrade" {
  description = "Enable to allow major engine version upgrades when changing engine versions."
  default     = false
}

variable "rds_apply_immediately" {
  description = "Whether or not modifications are applied immediately."
  default     = true
}

variable "rds_availability_zones" {
  description = "Availability zones in which to launch cluster instances"
  type        = list(string)
  default     = ["us-east-1b", "us-east-1c"]
}

variable "rds_backup_retention_period" {
  description = "Number of days to retain backups"
  default     = "30"
}

variable "rds_backtrack_window" {
  description = "Target backtrack window in seconds. Check db engine for backtrack support before setting this to a non-zero value."
  default     = "0"
}

variable "rds_db_subnet_ids" {
  type        = list(string)
  description = "List of subnet IDs to run RDS instances in."
  default     = []

  validation {
    condition     = length(var.rds_db_subnet_ids) > 0 || var.rds_db_subnet_group_name != null
    error_message = "One of rds_db_subnet_ids or rds_db_subnet_group_name must be set."
  }
}

variable "rds_db_subnet_group_name" {
  description = "Set this to use a pre-existing subnet group rather than creating a new one."
  default     = null
}

variable "rds_db_cluster_parameter_group_family" {
  type        = string
  description = "The parameter group family to use for the cluster e.g. aurora-mysql8.0 or aurora-postgresql17"
  default     = null

  validation {
    condition     = var.rds_db_cluster_parameter_group_family != null || var.rds_db_cluster_parameter_group_name != null
    error_message = "One of rds_db_cluster_parameter_group_family or rds_db_cluster_parameter_group_name must be set."
  }
}

variable "rds_db_cluster_parameters" {
  type = list(object({
    name         = string
    value        = string
    apply_method = optional(string)
  }))
  description = "List of cluster parameter overrides to apply."
  default     = []
}

variable "rds_db_cluster_parameter_group_name" {
  description = "Set this to use a pre-existing cluster parameter group rather than creating a new one."
  default     = null
}

variable "rds_engine_mode" {
  description = "Engine mode: supported options are global, provisioned, parallelquery, and serverless. Note that not all modes are supported with every engine"
  default     = "provisioned"
}

variable "rds_engine" {
  description = "The engine to use, e.g. aurora, aurora-mysql, aurora-postgresql"
  default     = "aurora-mysql"
}

variable "rds_engine_version" {
  description = "Version of specified engine to use. It is strongly recommended to specify only a major version if automatic minor version upgrades are enabled (e.g. 8.0 for MySQL or 17 for Postgres), to prevent drift."
}

variable "rds_master_username" {
  description = "Master username. Leave blank; this should ONLY be passed in from command-line flags or a local file."
  default     = ""
}

variable "rds_master_password" {
  description = "Master password. Leave blank; this should ONLY be passed in from command-line flags or a local file."
  default     = ""
}

variable "rds_preferred_backup_window" {
  description = "Preferred daily window for automated backups (UTC)"
  default     = "07:00-09:00"
}

variable "rds_preferred_maintenance_window" {
  description = "Preferred window for weekly maintenance (UTC). Should be after a backup."
  default     = "sun:10:00-sun:11:00"
}

variable "rds_skip_final_snapshot" {
  description = "Whether or not to skip final snapshot"
  default     = true
}

# RDS serverless-specific variables
variable "rds_serverless_cluster_max_capacity" {
  description = "Maximum Aurora capacity unit"
  default     = 16
}

variable "rds_serverless_cluster_min_capacity" {
  description = "Minimum Aurora capacity unit"
  default     = 2
}

variable "rds_serverless_cluster_auto_pause_seconds" {
  description = "Number of seconds before cluster auto-pauses"
  default     = 300
}

# RDS instance variables
variable "rds_performance_insights_enabled" {
  description = "Whether or not performance insights are enabled. Currently not supported for parallel query instances"
  default     = true
}

variable "rds_cluster_instance_auto_minor_version_upgrade" {
  description = "Whether or not to automatically perform minor version upgrades to cluster instances."
  default     = true
}

variable "rds_cluster_instance_count" {
  description = "Number of instances to launch"
  default     = "2"
}

variable "rds_cluster_instance_class" {
  description = "Instance class to launch"
  default     = "db.t4g.medium"
  validation {
    condition = contains(
      [
        "db.t4g.micro", "db.t4g.small", "db.t4g.medium", "db.t4g.large", "db.t4g.xlarge", "db.t4g.2xlarge",
        "db.r6g.large", "db.r6g.xlarge", "db.r6g.2xlarge", "db.r6g.4xlarge", "db.r6g.8xlarge", "db.r6g.12xlarge",
        "db.r7g.large", "db.r7g.xlarge", "db.r7g.2xlarge", "db.r7g.4xlarge", "db.r7g.8xlarge", "db.r7g.16xlarge",
      ], var.rds_cluster_instance_class
    )
    error_message = "Invalid instance class. Please refer to the module documentation for valid instance classes."
  }
}

variable "rds_cluster_instance_class_secondary" {
  description = "Instance class for the secondary (read-only) endpoints"
  type        = string
  default     = "none"
  validation {
    condition = var.rds_cluster_instance_class_secondary == "none" || contains(
      [
        "db.t4g.micro", "db.t4g.small", "db.t4g.medium", "db.t4g.large", "db.t4g.xlarge", "db.t4g.2xlarge",
        "db.r6g.large", "db.r6g.xlarge", "db.r6g.2xlarge", "db.r6g.4xlarge", "db.r6g.8xlarge", "db.r6g.12xlarge",
        "db.r7g.large", "db.r7g.xlarge", "db.r7g.2xlarge", "db.r7g.4xlarge", "db.r7g.8xlarge", "db.r7g.16xlarge",
      ],
      var.rds_cluster_instance_class_secondary
    )
    error_message = "Invalid instance class. Please refer to the module documentation for valid instance classes."
  }
}

variable "rds_cluster_instance_monitoring_interval" {
  description = "Interval, in seconds, for polling cluster instances for metrics"
  default     = 5
}

variable "rds_cluster_instance_publicly_accessible" {
  description = "Whether or not cluster instances are publicly accessible. This normally should not be changed."
  default     = false
}

variable "rds_cluster_instance_parameter_group_family" {
  type        = string
  description = "The parameter group family to use for the cluster instances e.g. aurora-mysql8.0 or aurora-postgresql17"
  default     = null

  validation {
    condition     = var.rds_cluster_instance_parameter_group_family != null || var.rds_cluster_instance_parameter_group_name != null
    error_message = "One of rds_cluster_instance_parameter_group_family or rds_cluster_instance_parameter_group_name must be set."
  }
}

variable "rds_cluster_instance_parameters" {
  type = list(object({
    name         = string
    value        = string
    apply_method = optional(string)
  }))
  description = "List of instance parameter overrides to apply."
  default     = []
}

variable "rds_cluster_instance_parameter_group_name" {
  description = "Set this to use a pre-existing instance parameter group rather than creating a new one."
  default     = null
}

variable "rds_db_cluster_iam_roles" {
  description = "List of IAM role ARNs to attach to the RDS instance cluster"
  default     = []
}

variable "rds_ingress_cidr_blocks" {
  description = "CIDR blocks from which to allow traffic"
  type        = list(string)
  default     = []
}

variable "rds_ingress_prefix_list_names" {
  description = "Prefix lists from which to allow traffic"
  type        = list(string)
  default     = []
}

variable "rds_ingress_security_groups" {
  description = "Security groups from which to allow traffic"
  type        = list(string)
  default     = []
}

variable "rds_port" {
  description = "Port for rds instance listener."
  default     = ""
}

variable "route53_ttl" {
  default = "60"
}

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

variable "rds_copy_tags_to_snapshot" {
  description = "Copy all Instance tags to snapshots"
  default     = true
}

variable "sns_kms_master_key_id" {
  description = "The ID of an AWS-managed customer master key (CMK) for Amazon SNS or a custom CMK."
  default     = "alias/aws/sns"
}

variable "enabled_cloudwatch_logs_exports" {
  description = "List of types of logs that being exported to cloudwatch"
  default     = ["audit", "error", "general", "slowquery"]
}

variable "custom_kms_key_enabled" {
  description = "Whether or not to use a customer managed KMS key."
  type        = bool
  default     = true
}

variable "account_ids" {
  description = "Account IDs to share RDS cluster"
  default = [
    "103233932089", # dev
    "079637511089"  # backup
  ]
}

variable "deletion_protection" {
  description = "Enable or disable deletion protection"
  type        = bool
  default     = null
}

variable "iam_database_authentication_enabled" {
  description = "Enable or disable IAM authentication"
  type        = bool
  default     = false
}

variable "snapshot_identifier" {
  description = "Specifies whether or not to create this cluster from a snapshot. You can use either the name or ARN when specifying a DB cluster snapshot, or the ARN when specifying a DB snapshot."
  type        = string
  default     = null
}

variable "enable_local_write_forwarding" {
  description = "Enable or disable local write forwarding"
  type        = bool
  default     = false
}

# rds proxy
variable "rds_proxy_enabled" {
  type        = bool
  description = "Enable RDS Proxy"
  default     = false
}

variable "rds_proxy_debug_logging" {
  type        = bool
  description = "Whether to enable debug logging for the RDS proxy"
  default     = false
}

variable "rds_proxy_engine_family" {
  type        = string
  description = "The engine family of the RDS proxy. Valid values are MYSQL or POSTGRESQL"
  default     = "MYSQL"
}

variable "rds_proxy_secrets" {
  type        = list(string)
  description = "The secrets to use for the RDS proxy"
  default     = []
  validation {
    condition     = !(var.rds_proxy_enabled && length(var.rds_proxy_secrets) == 0)
    error_message = "RDS Proxy requires at least one secret to be provided"
  }
}

variable "rds_proxy_idle_client_timeout" {
  type        = number
  description = "The number of seconds that a connection to the proxy can be idle before the proxy disconnects it"
  default     = 1800
}

variable "rds_proxy_require_tls" {
  type        = bool
  description = "Whether to require TLS for connections to the proxy"
  default     = false
}

variable "rds_proxy_connection_borrow_timeout" {
  type        = number
  description = "The number of seconds that a connection to the proxy can be borrowed before the proxy closes it"
  default     = 120
}

variable "rds_proxy_init_query" {
  type        = string
  description = "The SQL statement that the proxy runs when a new connection is opened"
  default     = null
}

variable "rds_proxy_max_connections_percent" {
  type        = number
  description = "The maximum number of connections that the proxy can establish to the DB instance"
  default     = 50
}

variable "rds_proxy_max_idle_connections_percent" {
  type        = number
  description = "The maximum number of idle connections that the proxy can maintain to the DB instance"
  default     = 25
}

variable "rds_proxy_session_pinning_filters" {
  type        = list(string)
  description = "The criteria that the proxy uses to determine whether to send a session to a target"
  default     = null
}

variable "custom_cmk_key_arn" {
  description = "Optional ARN for a pre-existing KMS key to use for encryption"
  type        = string
  default     = null
}

variable "permissions_boundary_enabled" {
  type        = bool
  description = <<EOF
  Whether or not to set a permissions boundary to limit the permissions of IAM roles created by this module.
  By default, this is true in dev (to allow local Terraform execution) and false in other environments.
EOF
  default     = null
}

variable "permissions_boundary_policy_name" {
  type        = string
  description = "The name of the policy to use as the permissions boundary for the IAM roles created by this module, if permissions_boundary_enabled is true."
  default     = "dev-permissions-boundary-policy"
}

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