module "default_tags" {
  source             = "git@github.com:theorchard/terraform-default-tags.git//?ref=1.0.0"
  environment        = var.environment
  application_family = var.application_family
  service_name       = var.service_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 {
  backend "s3" {
    bucket  = "orcd-terraform-state"
    key     = "prod/docs-swagger-ui/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" "networking_route53_zone" {
  provider = aws.networking

  name = "theorchard.io"
}

module "docs_swagger_ui_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                           = "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"
  vpc_id                                   = module.vpc_info.vpc_id
  https_listener_certificate_id            = split("/", data.aws_acm_certificate.theorchard_io.arn)[1]
  health_check_path                        = "/hello/"

  https_listener_allow_prefix_list_names = [
    "vpn-ny-users",
  ]

  fargate_service_subnets = module.vpc_info.default_private_subnet_ids

  load_balancer_subnets = module.vpc_info.default_private_subnet_ids

  environment_variables = [
    {
      Environment = var.environment
    },
    {
      AWS_REGION = var.aws_region
    },
    {
      DATADOG_ENV = "${var.environment}-${var.service_name}"
    },
    {
      DD_LOGS_INJECTION = "true"
    }
  ]
}

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
  service_4xx_monitor_enabled = false
  service_5xx_monitor_enabled = false

  notification_endpoints            = ""
  escalation_notification_endpoints = ""
}

# Create a vanity DNS name
resource "aws_route53_record" "swagger_dns_record" {
  zone_id = data.aws_route53_zone.route53_zone.zone_id
  name    = "swagger"
  type    = "CNAME"
  ttl     = "300"
  records = ["${module.docs_swagger_ui_fargate_environment.fargate_load_balancer_dns_name}"]
}

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

  zone_id = data.aws_route53_zone.networking_route53_zone.zone_id
  name    = "swagger"
  type    = "CNAME"
  ttl     = "300"
  records = ["${module.docs_swagger_ui_fargate_environment.fargate_load_balancer_dns_name}"]
}
