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

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

variable "service_name" {
  default = "vector-delivery"
}

variable "application_family" {
  default = "vector"
}

variable "team_name" {
  type        = string
  description = "Name of team that owns the service"
  default     = "content-delivery-asset-management"
}

variable "app_log_level" {
  type        = string
  description = "Application log level"
  default     = "Warning"
}

variable "additional_tags" {
  type = map(string)
  default = {
    "map-migrated" = "d-server-037ku7rhxwbcc1"
  }
}

variable "s3_video_bucket" {
  default = "prod-orcd-video-assets"
}

variable "s3_mezz_bucket" {
  default = "prod-orcd-mezzanine-assets"
}

variable "video_encoder_id" {
  default = "17"
}

variable "audio_encoder_id" {
  default = "18"
}

variable "physical_encoder_id" {
  default = "23"
}

variable "physical_location_ids" {
  type        = set(string)
  description = "List of physical location IDs to use when creating resources"
  default = [
    "13", # AWS Prod S3
  ]
}

variable "healthy_task_monitors" {
  type = set(object({
    physical_location               = string,
    worker_type                     = string,
    notification                    = string,
    escalation                      = string,
    healthy_tasks_evaluation_window = string,
  }))
  default = [
    {
      physical_location               = "13",
      worker_type                     = "audio",
      notification                    = "@slack-content-delivery-and-asset-mgmt-alerts",
      escalation                      = "@oncall-content-delivery-asset-management",
      healthy_tasks_evaluation_window = "last_1h",
    },
    {
      physical_location               = "13",
      worker_type                     = "video",
      notification                    = "@slack-content-delivery-and-asset-mgmt-alerts",
      escalation                      = "@oncall-content-delivery-asset-management",
      healthy_tasks_evaluation_window = "last_1h",
    },
    {
      physical_location               = "13",
      worker_type                     = "physical",
      notification                    = "@slack-content-delivery-and-asset-mgmt-alerts",
      escalation                      = "@oncall-content-delivery-asset-management",
      healthy_tasks_evaluation_window = "last_2h",
    },
  ]
}

variable "audio_service_name_to_encoding_priority" {
  type = set(object({
    encoding_priority = string,
    min_task_count    = string,
    max_task_count    = string,
    scaling_threshold = optional(number, 50)
  }))
  default = [
    {
      encoding_priority = "1"
      min_task_count    = "5"
      max_task_count    = "384"
    },
    {
      encoding_priority = "2"
      min_task_count    = "5"
      max_task_count    = "512"
    },
    {
      encoding_priority = "3"
      min_task_count    = "5"
      max_task_count    = "384"
    },
    {
      encoding_priority = "4"
      min_task_count    = "5"
      max_task_count    = "512"
    },
    {
      encoding_priority = "5"
      min_task_count    = "5"
      max_task_count    = "768"
    },
    {
      encoding_priority = "6"
      min_task_count    = "5"
      max_task_count    = "384"
      # Use lower scaling threshold for p6 to ensure throughput when there is
      # a large backlog of jobs for a single store only - see CDAM-3395
      scaling_threshold = 10
    }
  ]
}

variable "video_service_name_to_encoding_priority" {
  type = set(object({
    encoding_priority = string,
    min_task_count    = string,
    max_task_count    = string,
  }))
  default = [
    {
      encoding_priority = "1"
      min_task_count    = "5"
      max_task_count    = "64"
    },
    {
      encoding_priority = "2"
      min_task_count    = "5"
      max_task_count    = "64"
    },
    {
      encoding_priority = "3"
      min_task_count    = "5"
      max_task_count    = "64"
    },
    {
      encoding_priority = "4"
      min_task_count    = "5"
      max_task_count    = "64"
    },
    {
      encoding_priority = "5"
      min_task_count    = "5"
      max_task_count    = "64"
    },
    {
      encoding_priority = "6"
      min_task_count    = "5"
      max_task_count    = "64"
    }
  ]
}

variable "physical_service_name_to_encoding_priority" {
  type = set(object({
    encoding_priority = string,
    min_task_count    = string,
    max_task_count    = string,
  }))
  default = [
    {
      encoding_priority = "1,2,3,4,5,6"
      min_task_count    = "8"
      max_task_count    = "8"
    },
  ]
}

variable "secrets_manager_secret_names" {
  type        = set(string)
  description = "List of secret names to create in secrets manager with the same settings"
  default = [
    "AR_DB_PASSWORD",
    "DD_DB_RO_PASSWORD",
    "DD_DB_RW_PASSWORD",
    "DD_DB_EO_PASSWORD",
    "AWS_KEY_ID",
    "AWS_SECRET_KEY",
    "DATADOG_API_KEY",
  ]
}

# Scaling variables
variable "scaling_cooldown_period_seconds" {
  description = "Scaling cooldown period in seconds."
  default     = 30
}

variable "audio_scale_up_adjustment" {
  description = "Number of tasks to add in response to cloudwatch alarm."
  default     = 128
}

variable "audio_scale_down_adjustment" {
  description = "Number of tasks to remove when alarm is in OK status."
  default     = -32
}

variable "video_scale_up_adjustment" {
  description = "Number of tasks to add in response to cloudwatch alarm."
  default     = 8
}

variable "video_scale_down_adjustment" {
  description = "Number of tasks to remove when alarm is in OK status."
  default     = -4
}

variable "s3_bucket_direct_delivery" {
  type        = string
  description = "Types of assets to use for S3 bucket lookup"
  default     = "direct-delivery"
}

variable "efs_mount_path" {
  description = "Path inside container to mount EFS volume"
  default     = "/mnt/efs"
}

locals {
  tags = {
    environment        = var.environment
    service_name       = var.service_name
    application_family = var.application_family
    terraformed        = true
  }

  flattened_audio_service_list = [
    for obj in setproduct(var.physical_location_ids, var.audio_service_name_to_encoding_priority) : {
      physical_location = obj[0]
      encoding_priority = obj[1]["encoding_priority"]
      min_task_count    = obj[1]["min_task_count"]
      max_task_count    = obj[1]["max_task_count"]
      scaling_threshold = obj[1]["scaling_threshold"]
    }
  ]

  flattened_video_service_list = [
    for obj in setproduct(var.physical_location_ids, var.video_service_name_to_encoding_priority) : {
      physical_location = obj[0]
      encoding_priority = obj[1]["encoding_priority"]
      min_task_count    = obj[1]["min_task_count"]
      max_task_count    = obj[1]["max_task_count"]
    }
  ]

  flattened_physical_service_list = [
    for obj in setproduct(var.physical_location_ids, var.physical_service_name_to_encoding_priority) : {
      physical_location = obj[0]
      encoding_priority = obj[1]["encoding_priority"]
      min_task_count    = obj[1]["min_task_count"]
      max_task_count    = obj[1]["max_task_count"]
    }
  ]

  pre_existing_policy_names = [
    "KMS-${var.environment}_itunes_trans_accounts-fullaccess",
    "S3-orcd-film-tv-vector-delivery-RW",
    "S3-${var.environment}-asset-storage-RW",
    "S3-transporter-logs-RW",
    "SecretsManager-${var.environment}-direct_delivery-connection_info-read-write-policy",
    "SES-SendOnly",
    "S3-${var.environment}-vector-audit-RW",
  ]

  s3_policies_to_attach = concat(
    [
      for policy in var.physical_location_ids : {
        policy_name = "S3-${var.environment}-vector-physical-location-${policy}-${var.s3_bucket_direct_delivery}-RW"
      }
    ]
  )
}
