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.region

  default_tags {
    tags = module.default_tags.tags
  }
}

terraform {
  backend "s3" {
    bucket  = "qa-accounting-terraform-state"
    key     = "uat/graphql-tax-payment/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

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

  environment = var.environment
}

data "aws_acm_certificate" "theorchard_io" {
  domain   = "*.qa-accounting.theorchard.io"
  statuses = ["ISSUED"]
}

data "aws_route53_zone" "route53_zone" {
  name = "qa-accounting.theorchard.io"
}

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

  providers = {
    aws.dns = aws
  }

  environment                              = var.environment
  service_name                             = var.service_name
  aws_region                               = var.region
  commit_sha                               = "latest"
  container_port                           = "8080"
  task_type                                = "web_service"
  desired_task_count                       = 0
  task_cpu                                 = 1024
  task_memory                              = 2048
  maximum_capacity                         = 4
  minimum_capacity                         = 0
  route53_zone_id                          = data.aws_route53_zone.route53_zone.zone_id
  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
  https_listener_allow_prefix_list_names   = [
    "uat-orcd-private-subnet-prefix-list",
  ]

  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
    },
    {
      NODE_ENV = var.environment
    },
    {
      LOGGER_DSN = "https://qa-fluentd-applications.theorchard.io:8888/application"
    },
    {
      INTROSPECTION = var.introspection
    },
    {
      PLAYGROUND = var.playground
    },
    {
      DEBUG = var.debug
    },
    {
      DD_LOGS_INJECTION = true
    },
    {
      OWS_PRODUCT_URL = "https://qa-ows-product.theorchard.io/"
    },
    {
      OWS_PAYEE_URL = "https://uat-ows-payee.theorchard.io/"
    },
    {
      AUTH0_DOMAIN = "qa-orchard.auth0.com"
    }
  ]

  secrets = [
    {
      SPLIT_API_KEY = "${var.environment}/split/API_KEY"
    }
  ]
}

module "graphql_tax_payment_service_dashboard" {
  source                            = "git@github.com:theorchard/terraform-datadog.git//modules/service?ref=6.13.0"
  environment                       = var.environment
  environment_type                  = "fargate"
  service_name                      = var.service_name
  notification_endpoints            = "@slack-monitoring @slack-graphql @slack-The_Orchard-documents-public"
  datadog_service_type              = "${var.service_name}-graphql"
  datadog_service_name              = "graphql.execute"
  service_4xx_monitor_enabled       = false
  service_5xx_monitor_enabled       = false
  application_family                = var.application_family
  escalation_notification_endpoints = "@slack-monitoring @slack-graphql @slack-The_Orchard-documents-public"
}

module "sentry_graphql_tax_payment" {
  source = "git@github.com:theorchard/terraform-sentry.git?ref=4.1.2"

  environment        = var.environment
  platform           = "javascript"
  service_name       = var.service_name
  teams              = ["abacus-qa"]
  application_family = var.application_family
}
