module "default_tags" {
  source             = "git@github.com:theorchard/terraform-default-tags.git//?ref=2.0.0"
  environment        = var.environment
  application_family = var.application_family
  service_name       = var.service_name
  team_name          = var.team
}

provider "aws" {
  region = var.aws_region

  default_tags {
    tags = module.default_tags.tags
  }
}

provider "aws" {
  region  = var.aws_region
  alias   = "networking"
  profile = "networking"

  default_tags {
    tags = module.default_tags.tags
  }
}

# Terraform backends cannot contain interpolations
terraform {
  backend "s3" {
    bucket  = "orcd-terraform-state"
    key     = "prod/promo-theorchard-com/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

module "vpc_info" {
  source = "git@github.com:theorchard/terraform-vpc-info.git//?ref=3.1.0"

  environment = var.environment
}

data "aws_acm_certificate" "theorchard_com" {
  domain   = "*.theorchard.com"
  statuses = ["ISSUED"]
}

data "aws_route53_zone" "route53_zone" {
  name = "theorchard.io"
}

data "aws_wafv2_web_acl" "public_shared_waf" {
  name  = "prod-public-shared-regional-waf-block"
  scope = "REGIONAL"
}

module "secrets" {
  source   = "git@github.com:theorchard/terraform-secrets-manager.git//?ref=1.5.1"
  for_each = toset(var.secrets_manager_secret_names)

  environment        = var.environment
  service_name       = var.service_name
  secret_name        = each.value
  application_family = var.application_family
}

module "promo_theorchard_com_fargate_environment" {
  source = "git@github.com:theorchard/terraform-fargate.git//?ref=5.6.1"

  providers = {
    aws.dns = aws.networking
  }

  environment                              = var.environment
  service_name                             = var.service_name
  aws_region                               = var.aws_region
  application_family                       = var.application_family
  commit_sha                               = "latest"
  container_port                           = "3000"
  task_type                                = "web_service"
  desired_task_count                       = 2
  task_cpu                                 = 512
  task_memory                              = 1024
  maximum_capacity                         = 4
  minimum_capacity                         = 2
  route53_zone_id                          = data.aws_route53_zone.route53_zone.zone_id
  load_balancer_access_logs_s3_bucket_name = "orch-elb-logs"
  load_balancer_is_internal                = "false"
  vpc_id                                   = module.vpc_info.vpc_id
  https_listener_certificate_id            = split("/", data.aws_acm_certificate.theorchard_com.arn)[1]
  health_check_path                        = "/hello/"
  custom_waf_arn                           = data.aws_wafv2_web_acl.public_shared_waf.arn

  # Only allow access from Cloudflare IPs
  prod_https_listener_allow_cidr_blocks = [
    "173.245.48.0/20",
    "103.21.244.0/22",
    "103.22.200.0/22",
    "103.31.4.0/22",
    "141.101.64.0/18",
    "108.162.192.0/18",
    "190.93.240.0/20",
    "188.114.96.0/20",
    "197.234.240.0/22",
    "198.41.128.0/17",
    "162.158.0.0/15",
    "172.64.0.0/13",
    "131.0.72.0/22",
    "104.16.0.0/13",
    "104.24.0.0/14",
  ]

  fargate_service_subnets = module.vpc_info.default_private_subnet_ids

  load_balancer_subnets = module.vpc_info.default_public_subnet_ids

  environment_variables = [
    {
      Environment = var.environment
    },
    {
      AWS_REGION = var.aws_region
    },
    {
      DATADOG_ENV = "${var.environment}-${var.service_name}"
    },
    {
      DD_LOGS_INJECTION = "true"
    },
    {
      BFF_PROMO_PLAYER_URL = "https://bff-promo-player.theorchard.io"
    },
    {
      SCRIPT_SRC = "https://cdn.theorchard.io"
    },
    {
      IMG_SRC = "https://images.theorchard.io"
    },
    {
      DEFAULT_SRC = "https://${var.environment}-aws-wowza.theorchard.com,https://${var.environment}-aws-wowza.awal.com,https://${var.environment}-aws-wowza.sonymusic.com"
    },
    {
      MEDIA_SRC = "https://${var.environment}-aws-wowza.theorchard.com,https://${var.environment}-aws-wowza.awal.com,https://${var.environment}-aws-wowza.sonymusic.com"
    },
  ]
  secrets = [
    {
      SEGMENT_KEY = "${var.environment}/${var.service_name}/SEGMENT_KEY"
    },
    {
      SENTRY_DSN = "${var.environment}/${var.service_name}/SENTRY_DSN"
    },
  ]
}

module "fargate_service_dashboard" {
  source = "git@github.com:theorchard/terraform-datadog.git//modules/service?ref=6.13.4"

  environment                       = var.environment
  environment_type                  = "fargate"
  service_name                      = var.service_name
  application_family                = var.application_family
  critical_number_5xx               = var.critical_number_5xx
  critical_recovery_number_5xx      = var.critical_recovery_number_5xx
  warning_number_5xx                = var.warning_number_5xx
  warning_recovery_number_5xx       = var.warning_recovery_number_5xx
  notification_endpoints            = var.notification_endpoints
  escalation_notification_endpoints = var.escalation_notification_endpoints
  additional_tags                   = { service_type : "critical" }
  teams                             = [var.application_family]
  service_4xx_monitor_enabled       = false
}
