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_name
}

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/bff-feature-fm/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_io" {
  domain   = "*.theorchard.io"
  statuses = ["ISSUED"]
}

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

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

module "bff_feature_fm_owsrequest" {
  source           = "git@github.com:theorchard/terraform-owsrequest.git//?ref=1.1.0"
  environment_name = var.environment
  service_name     = var.service_name
}

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

  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                           = "8080"
  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"
  vpc_id                                   = module.vpc_info.vpc_id
  https_listener_certificate_id            = split("/", data.aws_acm_certificate.theorchard_io.arn)[1]
  load_balancer_is_internal                = false
  custom_waf_arn                           = data.aws_wafv2_web_acl.shared_regional_waf_block.arn
  iam_managed_policy_attachments = [
    module.bff_feature_fm_owsrequest.policy_arn_output
  ]

  prod_https_listener_allow_cidr_blocks = [
    "31.168.227.138/32",
    "34.201.121.156/32",
    "34.209.25.108/32",
    "34.211.203.180/32",
    "34.215.85.255/32",
    "34.216.250.225/32",
    "34.221.136.204/32",
    "35.164.215.210/32",
    "35.165.4.46/32",
    "52.88.73.41/32",
    "54.149.183.178/32",
    "54.200.151.194/32"
  ]

  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
    },
    {
      LOGGER_DSN = "https://${var.environment}-fluentd-applications.theorchard.io:8888/application"
    },
    {
      DATADOG_ENV = "${var.environment}-${var.service_name}"
    },
    {
      DD_LOGS_INJECTION = "true"
    },
    {
      SECRET_TOKEN = var.secret_token_path
    },
    {
      SECRET_TOKEN_KEY = var.secret_token_name
    }
  ]

  secrets = [
    {
      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
  notification_endpoints            = "@slack-feature-fm-alerts"
  escalation_notification_endpoints = "@slack-feature-fm-alerts"
  service_4xx_monitor_enabled       = true

  critical_number_5xx          = 20
  critical_recovery_number_5xx = 15
  warning_number_5xx           = 10
  warning_recovery_number_5xx  = 5
}
