# General settings
variable "environment" {
  type        = string
  description = "Name of environment, eg. dev, qa, prod"
  default     = "dev"
}

variable "service_name" {
  type        = string
  description = "Name of service, e.g. publishing, content, podcast"
  default     = "service"
}

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 "brand" {
  type        = string
  description = "Name of brand to use for lookups and naming."
}

# S3 Settings
variable "custom_origin_primary_s3_bucket_name" {
  type        = string
  description = "Name of custom S3 bucket for use as primary origin bucket"
  default     = ""
}

variable "custom_origin_failover_s3_bucket_name" {
  type        = string
  description = "Name of custom S3 bucket for use as failover origin bucket"
  default     = ""
}

variable "s3_origin_group_name" {
  type        = string
  description = "Name of S3 origin group"
  default     = "s3_origin_group"
}

variable "custom_s3_origin_path" {
  type        = string
  description = "Custom path inside S3 origin to use as root prefix. Should start with /"
  default     = ""
}

# Alias and Certificate Settings
variable "domain_name" {
  type        = string
  description = "Name of domain for ACM certificate lookup and alias configuration"
  default     = "dev.theorchard.io"
}

variable "acm_certificate_cn" {
  type        = string
  description = "CN to use for ACM certificate lookup"
  default     = "*"
}

variable "additional_aliases" {
  type        = list(string)
  description = "Extra CNAMEs for this distribution. Should be in domain specified by var.domain_name for TLS certificate validity"
  default     = []
}

variable "use_only_additional_aliases" {
  type        = bool
  description = "Whether to ONLY configure value of additional_aliases for app and disable pre-generated CNAME"
  default     = false
}

# WAF settings
variable "custom_waf_name" {
  type        = string
  description = "Name of Cloudfront-scoped WAF to use for distribution if it does not match var.environment-var.brand-cloudfront-waf-block naming convention"
  default     = ""
}

# Cloudfront Function settings
variable "cloudfront_function_runtime" {
  type        = string
  description = "Name of Cloudfront Function runtime"
  default     = "cloudfront-js-1.0"
}

variable "cloudfront_function_publish_changes_live" {
  type        = bool
  description = "Whether or not to publish changes to Cloudfront Function as live version"
  default     = true
}

variable "cloudfront_function_file_path" {
  type        = string
  description = "Location of Cloudfront function code; if not using default, specify a path using this variable. e.g. path.module/file.js"
  default     = ""
}

# Cloudfront Distbitution settings
variable "default_root_object" {
  type        = string
  description = "The object that you want CloudFront to return on root url"
  default     = "index.html"
}

variable "enabled" {
  description = "Whether distribution is enabled"
  type        = bool
  default     = true
}

variable "http_version" {
  type        = string
  description = "The maximum HTTP version to support. Allowed values are http1.1 and http2. This should not normally be changed."
  default     = "http2"
}

variable "is_ipv6_enabled" {
  type        = bool
  description = "Whether IPv6 is enabled"
  default     = true
}

variable "price_class" {
  type        = string
  description = "The price class for this distribution. One of PriceClass_All, PriceClass_200, PriceClass_100. This should not normally be changed."
  default     = "PriceClass_All"
}

variable "allowed_methods" {
  type        = list(string)
  description = "Allowed HTTP methods for distribution"
  default     = ["HEAD", "GET"]
}

variable "cached_methods" {
  type        = list(string)
  description = "Cached HTTP methods for Cloudfront"
  default     = ["HEAD", "GET"]
}

variable "default_ttl" {
  type        = string
  description = "Default TTL for objects in absence of cache headers"
  default     = "86400"
}

variable "minimum_ttl" {
  type        = number
  description = "Minimum TTL for default cache behavior"
  default     = 0
}

variable "maximum_ttl" {
  type        = number
  description = "Maximum TTL for default cache behavior"
  default     = 604800
}

variable "minimum_protocol_version" {
  type        = string
  description = "Minimum SSL protocol"
  default     = "TLSv1.2_2021"
}

variable "failover_criteria_status_codes" {
  type        = list(number)
  description = "List of status codes that comprise S3 origin failover criteria"
  default = [
    403,
    500,
    502,
    503,
  ]
}

variable "viewer_protocol_policy" {
  type        = string
  description = "Protocol allowed to access origins. One of allow-all, https-only, or redirect-to-https."
  default     = "redirect-to-https"
}

variable "forward_cookies_to_origin_setting" {
  type        = string
  description = "How to forward cookies to origin. Set to all, none, or whitelist"
  default     = "none"
}

variable "list_of_cookies_to_forward" {
  type        = list(string)
  description = "List of cookies to forward to origin. Only applicable when var.forward_cookies_to_origin_setting is set to whitelist"
  default     = []
}

variable "list_of_headers_to_forward" {
  type        = list(string)
  description = "List of headers to forward to origin"
  default     = []
}

variable "forward_query_strings_to_origin" {
  type        = bool
  description = "Whether to forward query strings to origin"
  default     = false
}

# Logging settings
variable "custom_logging_bucket" {
  type        = string
  description = "Custom bucket to use for Cloudfront logging. Set if overriding default."
  default     = ""
}

variable "logging_include_cookies" {
  type        = bool
  description = "Whether or not to include cookies in logs"
  default     = true
}

# Response policy headers settings
variable "response_headers_policy_enabled" {
  type        = bool
  description = "Whether or not response_headers_policy is enabled and attached"
  default     = true
}

variable "custom_response_headers_policy_name" {
  type        = string
  description = "Name of custom response header policy. Default is var.environment-default"
  default     = ""
}

variable "strict_transport_security_max_age_seconds" {
  type        = number
  description = "HSTS access-control max age in seconds"
  default     = 365 * 24 * 60 * 60
}

# Restriction settings
variable "restriction_type" {
  type        = string
  description = "Whether to restrict content by country. Set to one of none, whitelist, or blacklist."
  default     = "none"
}

variable "restriction_list_of_countries" {
  type        = list(string)
  description = "List of countries for Cloudfront to distribute (when var.restriction_type is whitelist) or not distribute (when var.restriction_type is blacklist) content. Not applicable when var.restriction_type is none"
  default     = []
}

variable "enable_centralized_cf_logs" {
  type        = bool
  description = "Send cloudfront logs to the centralized S3 bucket shared-orcd-cloudfront-logs in the shared AWS account. Variable custom_logging_bucket is ignored if this parameter is set to true."
  default     = true
}

locals {
  origin_primary_s3_bucket_name  = var.custom_origin_primary_s3_bucket_name != "" ? var.custom_origin_primary_s3_bucket_name : "${var.environment}-orcd-cdn"
  origin_failover_s3_bucket_name = var.custom_origin_failover_s3_bucket_name != "" ? var.custom_origin_failover_s3_bucket_name : "${var.environment}-orcd-cdn-us-west01"
  waf_name                       = var.custom_waf_name != "" ? var.custom_waf_name : "${var.environment}-${var.brand}-cloudfront-waf-block"
  s3_origin_path                 = var.custom_s3_origin_path != "" ? var.custom_s3_origin_path : "/frontend-${var.service_name}"
  logging_bucket                 = var.enable_centralized_cf_logs ? "shared-orcd-cloudfront-logs" : var.custom_logging_bucket != "" ? var.custom_logging_bucket : "${var.environment}-orcd-cloudfront-logs"
  logging_bucket_domain_name     = "${local.logging_bucket}.s3.amazonaws.com"
  cloudfront_function_file_path  = var.cloudfront_function_file_path != "" ? var.cloudfront_function_file_path : "${path.module}/functions/viewer-request.js"
  response_headers_policy_name   = var.custom_response_headers_policy_name != "" ? var.custom_response_headers_policy_name : "${var.environment}-default"

  # Concatenate required and user-supplied tags
  combined_resource_tags = merge(
    {
      environment        = var.environment
      service_name       = var.service_name
      application_family = var.application_family
      terraformed        = true
    },
    var.additional_tags
  )

  # Concatenate aliases
  aliases = var.use_only_additional_aliases ? var.additional_aliases : concat(
    ["${var.service_name}.${var.domain_name}"],
    var.additional_aliases,
  )
}
