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

data "aws_s3_bucket" "logging_bucket" {
  bucket = "${var.environment}-fansifter-cloudfront-logs"
}

data "aws_acm_certificate" "certificate" {
  domain   = "*.theorchard.io"
  statuses = ["ISSUED"]
}

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

data "aws_cloudfront_origin_request_policy" "origin_request_policy" {
  name = "Managed-AllViewerAndCloudFrontHeaders-2022-06"
}

resource "aws_cloudfront_response_headers_policy" "response_headers_policy" {
  name    = "${var.environment}-${var.service_name}"
  comment = "${var.environment}-${var.service_name}"

  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_acm_certificate" "certificate" {
  domain_name               = "fan-preferences.${var.domain_name}"
  validation_method         = "DNS"

  options {
    certificate_transparency_logging_preference = "ENABLED"
  }

  tags = {
    environment  = var.environment
    service_name = var.service_name
    terraformed  = "true"
  }

  lifecycle {
    create_before_destroy = true
  }
}

resource "aws_acm_certificate_validation" "certificate_validation" {
  certificate_arn         = aws_acm_certificate.certificate.arn
  validation_record_fqdns = aws_acm_certificate.certificate.domain_validation_options[*].resource_record_name
}

resource "aws_cloudfront_distribution" "distribution" {
  # checkov:skip=CKV2_AWS_47:Rule requires the AWSManagedRulesAnonymousIpList to be enabled on the WAF, which blocks legitimate traffic.
  # checkov:skip=CKV_AWS_310:Temporarily silenced.
  # checkov:skip=CKV_AWS_374:We do not want geo restrictions on this distribution

  comment = "Distribution for ${var.environment}-${var.service_name}"
  aliases = [
    "fan-preferences.${var.domain_name}",
  ]
  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 = "${var.environment}-${var.service_name}.theorchard.io"
    origin_id   = "alb_origin"

    custom_origin_config {
      http_port                = 4443 # dummy, https only
      https_port               = 443
      origin_protocol_policy   = "https-only"
      origin_ssl_protocols     = ["TLSv1.2"]
      origin_keepalive_timeout = 60
      origin_read_timeout      = 60
    }
  }

  default_cache_behavior {
    target_origin_id           = "alb_origin"
    allowed_methods            = ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"]
    cached_methods             = ["HEAD", "GET", "OPTIONS"]
    cache_policy_id            = data.aws_cloudfront_cache_policy.cache_policy_caching_disabled.id
    origin_request_policy_id   = data.aws_cloudfront_origin_request_policy.origin_request_policy.id
    response_headers_policy_id = aws_cloudfront_response_headers_policy.response_headers_policy.id
    viewer_protocol_policy     = "redirect-to-https"
  }

  restrictions {
    geo_restriction {
      restriction_type = "none"
    }
  }

  viewer_certificate {
    acm_certificate_arn      = aws_acm_certificate.certificate.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.service_name}"
  }

  tags = local.tags
}

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

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

  name    = "${var.environment}-${var.subdomain_name}"
  zone_id = data.aws_route53_zone.theorchard_io_route53_zone.zone_id
  type    = "CNAME"
  ttl     = "60"
  records = [aws_cloudfront_distribution.distribution.domain_name]
}
