locals {
  s3_origin_id                            = "S3-${data.aws_s3_bucket.asset_storage.id}/images"
  distribution_domain                     = "${data.aws_s3_bucket.asset_storage.id}.s3.amazonaws.com"
  distribution_alias                      = "${var.environment}-images.theorchard.io"
  images_distribution_acm_certificate_arn = "arn:aws:acm:us-east-1:437795906767:certificate/a76e4883-28aa-4062-a4cc-c06b8012ae28"
  images_distribution_domain              = "theorchard.io"
}

data "aws_wafv2_web_acl" "waf" {
  name  = "${var.environment}-orcd-cloudfront-waf-block"
  scope = "CLOUDFRONT"
}

data "aws_cloudfront_response_headers_policy" "default_policy" {
  name = "${var.environment}-default"
}

resource "aws_cloudfront_origin_access_identity" "origin_access_identity" {
  comment = "access-identity-${local.distribution_domain}"
}

# CloudFront Distribution is imported resource.
resource "aws_cloudfront_distribution" "images_distribution" {
  # checkov:skip=CKV_AWS_310: Ensure CloudFront distributions should have origin failover configured
  # checkov:skip=CKV2_AWS_47: Ensure AWS CloudFront attached WAFv2 WebACL is configured with AMR for Log4j Vulnerability
  # checkov:skip=CKV_AWS_374: Ensure AWS CloudFront web distribution has geo restriction enabled
  enabled             = true
  is_ipv6_enabled     = true
  comment             = "Distribution for thumbnail images ${local.distribution_alias}"
  aliases             = ["${local.distribution_alias}"]
  price_class         = "PriceClass_All"
  http_version        = "http2"
  web_acl_id          = data.aws_wafv2_web_acl.waf.arn
  default_root_object = "index.html"

  origin {
    domain_name = local.distribution_domain
    origin_path = "/images"
    origin_id   = local.s3_origin_id

    s3_origin_config {
      origin_access_identity = aws_cloudfront_origin_access_identity.origin_access_identity.cloudfront_access_identity_path
    }
  }

  restrictions {
    geo_restriction {
      restriction_type = "none"
      locations        = []
    }
  }

  default_cache_behavior {
    allowed_methods            = ["HEAD", "GET"]
    cached_methods             = ["HEAD", "GET"]
    target_origin_id           = local.s3_origin_id
    min_ttl                    = 0
    max_ttl                    = 31536000
    default_ttl                = 86400
    viewer_protocol_policy     = "https-only"
    compress                   = false
    smooth_streaming           = false
    response_headers_policy_id = data.aws_cloudfront_response_headers_policy.default_policy.id

    forwarded_values {
      query_string = false

      cookies {
        forward = "none"
      }
    }
  }

  ordered_cache_behavior {
    path_pattern               = "product/*"
    allowed_methods            = ["HEAD", "GET"]
    cached_methods             = ["HEAD", "GET"]
    target_origin_id           = local.s3_origin_id
    min_ttl                    = 0
    max_ttl                    = 25
    default_ttl                = 25
    viewer_protocol_policy     = "https-only"
    compress                   = false
    smooth_streaming           = false
    response_headers_policy_id = data.aws_cloudfront_response_headers_policy.default_policy.id

    forwarded_values {
      query_string = false

      cookies {
        forward = "none"
      }
    }
  }

  ordered_cache_behavior {
    path_pattern               = "v2/product/*"
    allowed_methods            = ["HEAD", "GET"]
    cached_methods             = ["HEAD", "GET"]
    target_origin_id           = local.s3_origin_id
    min_ttl                    = 86400
    max_ttl                    = 31536000
    default_ttl                = 86400
    viewer_protocol_policy     = "https-only"
    compress                   = false
    smooth_streaming           = false
    response_headers_policy_id = data.aws_cloudfront_response_headers_policy.default_policy.id

    forwarded_values {
      query_string = false

      cookies {
        forward = "none"
      }
    }
  }

  custom_error_response {
    error_code            = 403
    error_caching_min_ttl = 300
    response_code         = 403
    response_page_path    = "/404"
  }

  custom_error_response {
    error_code            = 404
    error_caching_min_ttl = 300
    response_code         = 404
    response_page_path    = "/404"
  }

  viewer_certificate {
    ssl_support_method       = "sni-only"
    minimum_protocol_version = "TLSv1.2_2018"
    acm_certificate_arn      = local.images_distribution_acm_certificate_arn
  }

  logging_config {
    include_cookies = false
    bucket          = var.cloudfront_logging_bucket
    prefix          = "${var.environment}-${local.distribution_alias}"
  }

  lifecycle {
    ignore_changes = all
  }
}
