data "aws_cloudfront_cache_policy" "managed_cache_disabled" {
  name = "Managed-CachingDisabled"
}

data "aws_cloudfront_origin_request_policy" "forward_everything" {
  name = "Managed-AllViewer"
}

resource "aws_cloudfront_origin_access_identity" "images_api_uploaded_files_access_identity" {
  comment = "Cloudfront access identity for accessing bucket ${module.images_api_uploaded_files.bucket.id}"
}

resource "aws_cloudfront_distribution" "images_api" {
  enabled      = true
  price_class  = "PriceClass_All"
  http_version = "http2"
  aliases      = [module.domains[local.env_prefix]["images-api"]]
  web_acl_id   = data.aws_wafv2_web_acl.ioc_web_acl_global.arn

  logging_config {
    include_cookies = false
    bucket          = data.aws_s3_bucket.security_logs.bucket_domain_name
    prefix          = "cf/${local.name_prefix}-images-api"
  }

  default_cache_behavior {
    allowed_methods        = ["GET", "HEAD"]
    cached_methods         = ["GET", "HEAD"]
    target_origin_id       = "origin-alb-${aws_lb.main.name}"
    viewer_protocol_policy = "redirect-to-https"
    min_ttl                = "0"
    default_ttl            = "14400" // 4 hours
    max_ttl                = "14400" // 4 hours
    compress               = true

    forwarded_values {
      query_string = true

      cookies {
        forward = "none"
      }
    }
  }

  ordered_cache_behavior {
    path_pattern    = "/uploads/*"
    allowed_methods = ["GET", "HEAD"]
    cached_methods  = ["GET", "HEAD"]

    target_origin_id = "origin-bucket-${module.images_api_uploaded_files.bucket.id}"

    min_ttl                = "14400" // 4 hours
    default_ttl            = "14400" // 4 hours
    max_ttl                = "14400" // 4 hours
    compress               = true
    viewer_protocol_policy = "redirect-to-https"

    forwarded_values {
      query_string = false
      headers      = ["Origin"]

      cookies {
        forward = "none"
      }
    }
  }

  ordered_cache_behavior {
    path_pattern     = "/admin/*"
    allowed_methods  = ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"]
    cached_methods   = ["GET", "HEAD"]
    target_origin_id = "origin-alb-${aws_lb.main.name}"

    min_ttl                  = 0
    default_ttl              = 0
    max_ttl                  = 0
    compress                 = true
    cache_policy_id          = data.aws_cloudfront_cache_policy.managed_cache_disabled.id
    viewer_protocol_policy   = "redirect-to-https"
    origin_request_policy_id = data.aws_cloudfront_origin_request_policy.forward_everything.id
  }

  ordered_cache_behavior {
    path_pattern     = "/artists/by_gras_participant_ids/*"
    allowed_methods  = ["GET", "HEAD"]
    cached_methods   = ["GET", "HEAD"]
    target_origin_id = "origin-alb-${aws_lb.main.name}"

    min_ttl                = "14400" // 4 hours
    default_ttl            = "14400" // 4 hours
    max_ttl                = "14400" // 4 hours
    compress               = true
    viewer_protocol_policy = "redirect-to-https"

    forwarded_values {
      query_string = true

      cookies {
        forward = "none"
      }
    }
  }

  origin {
    domain_name = aws_lb.main.dns_name
    origin_id   = "origin-alb-${aws_lb.main.name}"

    connection_attempts = 3
    connection_timeout  = 10

    custom_header {
      name  = "X-Custom-Header"
      value = "random-string-${random_string.custom_header_prefix.result}"
    }

    custom_origin_config {
      http_port              = 80
      https_port             = 443
      origin_protocol_policy = "http-only"
      origin_ssl_protocols   = ["TLSv1.2"]
    }

    origin_shield {
      enabled              = true
      origin_shield_region = var.aws_region_id
    }
  }

  origin {
    origin_id   = "origin-bucket-${module.images_api_uploaded_files.bucket.id}"
    domain_name = module.images_api_uploaded_files.bucket.bucket_regional_domain_name

    s3_origin_config {
      origin_access_identity = aws_cloudfront_origin_access_identity.images_api_uploaded_files_access_identity.cloudfront_access_identity_path
    }
  }

  restrictions {
    geo_restriction {
      restriction_type = "none"
    }
  }

  viewer_certificate {
    acm_certificate_arn      = data.aws_acm_certificate.wildcard_atlas_stream_cert.arn
    ssl_support_method       = "sni-only"
    minimum_protocol_version = "TLSv1.2_2021"
  }

  tags = {
    project                  = "Core",
    service                  = "Cloudfront",
    plat_env_project_service = "${local.aggregated_tag}_CORE_CFR"
  }
}
