# General settings
variable "aws_region" {
  default     = "us-east-1"
  description = "AWS region."
}

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

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 "cluster_name" {
  default     = "managed-kafka-cluster"
  description = "MSK cluster name."
}

variable "kafka_version" {
  default     = "2.6.0"
  description = "Kafka version."
}

variable "kafka_num_nodes" {
  default     = 3
  description = "Number of nodes to run Kafka cluster."
}

variable "kafka_instance_type" {
  default     = "kafka.m5.large"
  description = "MSK instance type."
}

variable "kafka_ebs_volume_size" {
  default     = "1000"
  description = "EBS volume size for kafka nodes."
}

variable "kafka_storage_scaling_max_capacity" {
  description = "Maximum value for Kafka storage autoscaling"
  default     = "10000"
}

variable "kafka_storage_scaling_target_value" {
  description = "Target utilization value for kafka storage"
  default     = 75
}

variable "kafka_properties" {
  description = "Kafka configuration properties"
  default     = "kafka_properties.conf"
}

# Network settings
variable "subnet_ids" {
  description = "The IDs of the subnets in which to run the MSK cluster instances. This should match the number of instances"
  type        = list(string)

  validation {
    condition     = var.kafka_num_nodes % length(var.subnet_ids) == 0
    error_message = "The number of nodes must be a multiple of the number of subnets"
  }
}

variable "vpc_id" {
  description = "VPC id (default dev)"
  default     = "vpc-34dbfd51"
}

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

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

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

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

variable "kafka_allowed_custom_prefix_list_names" {
  description = "Prefix lists names from which to allow access to Kafka"
  type        = list(string)

  default = [
    "shared-orcd-atlantis-private-subnet-prefix-list",
  ]
}

variable "kafka_allowed_security_group_ids" {
    description = "Security Group IDs from which to allow access to Kafka"
    type        = list(string)
    default     = []
}

# Datadog loogging and monitoring settings
variable "datadog_enabled" {
  description = "Whether or not to send Kafka logs to Datadog"
  default     = false
}

variable "datadog_function_destination_lambda_name" {
  description = "Name of datadog function for shipping cloudwatch logs"
  default     = "DatadogLambdaFunction"
}

variable "datadog_custom_lambda_destination_arn" {
  description = "Custom ARN to use for sending Cloudwatch logs to Datadog. Only needed if overriding the default"
  default     = ""
}

# CloudWatch settings.
variable "log_retention_in_days" {
  description = "Amount of days that we store logs for in CloudWatch."
  type        = number
  default     = 365
}

variable "cloudwatch_enhanced_monitoring" {
  description = "Settings for CloudWatch enhanced monitoring."
  default     = "PER_TOPIC_PER_BROKER"
}

# Encryption settings
variable "in_cluster_encryption" {
  default     = true
  description = "Enable encryption in transit."
}

variable "in_transit_encryption" {
  default     = "TLS"
  description = "Encryption settings for data between client and brokers."
}

variable "kms_key_deletion_window" {
  description = "Key deletion period in days."
  default     = 30
}

locals {
  access_cidr_blocks = {
    dev  = var.dev_allowed_custom_cidr_blocks
    qa   = var.qa_allowed_custom_cidr_blocks,
    prod = var.prod_allowed_custom_cidr_blocks,
    uat  = var.uat_allowed_custom_cidr_blocks
   }

  kafka_allowed_prefix_list_names = concat(
    lookup({
      dev = [
        "vpn-ny-users"
      ]
      qa = [
        "vpn-ny-users"
      ]
      uat = [
        "vpn-ny-users"
      ]
    }, var.environment, []),
    [data.aws_ec2_managed_prefix_list.private_subnets.name]
  )

  sg_port_rules = flatten([
    for sg_id in var.kafka_allowed_security_group_ids : [
      for port in [9094, 2181, 2182] : {
        source_sg_id = sg_id
        from_port = port
        to_port = port
        protocol = "tcp"
      }
    ]
  ])




  datadog_function_destination_arn = var.datadog_custom_lambda_destination_arn != "" ? var.datadog_custom_lambda_destination_arn : "arn:aws:lambda:${var.aws_region}:${data.aws_caller_identity.current.account_id}:function:${var.datadog_function_destination_lambda_name}"

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

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

output "sg_port_rules" {
  value = local.sg_port_rules
}