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/ows-accounting/proxy/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_route53_zone" "route53_zone_networking" {
  provider = aws.networking

  name = "theorchard.io"
}

data "aws_caller_identity" "current" {}

data "aws_wafv2_web_acl" "public_shared_waf" {
  name  = "${var.environment}-public-shared-regional-waf-block"
  scope = "REGIONAL"
}

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

  providers = {
    aws.dns = aws.networking
  }

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

  prod_https_listener_allow_cidr_blocks = [
    "38.96.8.210/32",
    "38.96.23.128/27",
    "12.26.192.104/29",
    "207.237.185.2/32",
    "52.11.155.1/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
    }
  ]
}

module "fargate_service_dashboard" {
  source                            = "git@github.com:theorchard/terraform-datadog.git//modules/service?ref=6.13.6"
  environment                       = var.environment
  environment_type                  = "fargate"
  service_name                      = var.service_name
  application_family                = var.application_family
  service_4xx_monitor_enabled       = false
  notification_endpoints            = "@acc-squad"
  escalation_notification_endpoints = "@slack-monitoring @acc-squad"
  critical_number_5xx               = 20
  critical_recovery_number_5xx      = 15
  warning_number_5xx                = 10
  warning_recovery_number_5xx       = 5
}

resource "aws_route53_record" "rsgd_record" {
  zone_id = data.aws_route53_zone.route53_zone.zone_id
  name    = "rsgd.theorchard.io"
  type    = "CNAME"
  ttl     = "60"
  records = [module.ows_accounting_proxy_fargate_environment.fargate_load_balancer_dns_name]
}

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

  zone_id = data.aws_route53_zone.route53_zone_networking.zone_id
  name    = "rsgd.theorchard.io"
  type    = "CNAME"
  ttl     = "60"
  records = [module.ows_accounting_proxy_fargate_environment.fargate_load_balancer_dns_name]
}
