locals {
  distribution_domain = module.audience_campaign_assets_s3_bucket.s3_bucket_regional_domain_name_output
  s3_origin_id        = "${module.audience_campaign_assets_s3_bucket.s3_bucket_name_output}/"
}

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

data "aws_s3_bucket" "logging_bucket" {
  bucket = "prod-orcd-cloudfront-logs"
}

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

resource "aws_cloudfront_response_headers_policy" "response_headers_policy" {
  name    = "${var.environment}-${var.assets_subdomain}"
  comment = "${var.environment}-${var.assets_subdomain}.theorchard.io"

  security_headers_config {
    content_type_options {
      override = true
    }
    frame_options {
      frame_option = "DENY"
      override     = true
    }
    referrer_policy {
      referrer_policy = "strict-origin-when-cross-origin"
      override        = true
    }
    strict_transport_security {
      access_control_max_age_sec = 365 * 24 * 60 * 60
      override                   = true
    }
    xss_protection {
      protection = true
      mode_block = true
      override   = true
    }
  }
}

resource "aws_cloudfront_distribution" "assets_distribution" {
  # checkov:skip=CKV2_AWS_47:(from orcd-cdn)Rule requires the AWSManagedRulesAnonymousIpList to be enabled on the WAF, which blocks legitimate traffic.
  # checkov:skip=CKV_AWS_310: Temporary silenced before Fansifter squad prioritizes Campaign Builder epic
  # checkov:skip=CKV_AWS_374:We do not want geo restrictions on this distribution

  comment         = "Distribution for ${module.audience_campaign_assets_s3_bucket.s3_bucket_name_output}"
  aliases         = ["${var.environment}-${var.assets_subdomain}.theorchard.io"]
  enabled         = true
  is_ipv6_enabled = true
  http_version    = "http2"
  price_class     = "PriceClass_All"
  web_acl_id      = data.aws_wafv2_web_acl.waf.arn

  default_root_object = "index.html"

  origin {
    domain_name = local.distribution_domain
    origin_id   = local.s3_origin_id

    s3_origin_config {
      origin_access_identity = aws_cloudfront_origin_access_identity.assets_origin_access_identity.cloudfront_access_identity_path
    }
  }

  default_cache_behavior {
    target_origin_id       = local.s3_origin_id
    viewer_protocol_policy = "https-only"
    allowed_methods        = ["HEAD", "GET", "OPTIONS"]
    cached_methods         = ["HEAD", "GET"]
    default_ttl            = 86400
    min_ttl                = 0
    max_ttl                = 31536000
    compress               = false

    response_headers_policy_id = aws_cloudfront_response_headers_policy.response_headers_policy.id

    forwarded_values {
      query_string = false

      cookies {
        forward = "none"
      }
    }
  }

  restrictions {
    geo_restriction {
      restriction_type = "none"
    }
  }

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

  logging_config {
    include_cookies = false
    bucket          = data.aws_s3_bucket.logging_bucket.bucket_domain_name
    prefix          = "${var.environment}-${var.assets_subdomain}"
  }

  tags = local.tags
}

resource "aws_route53_record" "assets_route53_record" {
  name    = "${var.environment}-${var.assets_subdomain}"
  zone_id = data.aws_route53_zone.route53_zone.zone_id
  type    = "CNAME"
  ttl     = "60"
  records = [aws_cloudfront_distribution.assets_distribution.domain_name]
}

resource "aws_route53_record" "assets_networking_route53_record" {
  provider = aws.networking

  name    = "${var.environment}-${var.assets_subdomain}"
  zone_id = data.aws_route53_zone.networking_route53_zone.zone_id
  type    = "CNAME"
  ttl     = "60"
  records = [aws_cloudfront_distribution.assets_distribution.domain_name]
}

data "aws_iam_policy_document" "cloudfront_s3_policy_document" {
  statement {
    actions   = ["s3:GetObject"]
    resources = ["${module.audience_campaign_assets_s3_bucket.s3_bucket_arn_output}/*"]

    principals {
      type        = "AWS"
      identifiers = [aws_cloudfront_origin_access_identity.assets_origin_access_identity.iam_arn]
    }
  }
}
