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

  default_tags {
    tags = module.default_tags.tags
  }
}

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

  default_tags {
    tags = module.default_tags.tags
  }
}

terraform {
  backend "s3" {
    bucket  = "orcd-terraform-state"
    key     = "prod/graphql-content-review/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_iam_policy" "open_search_rw_policy" {
  name = "Elasticsearch-${var.environment}-${var.os_domain_name}-RW-policy"
}

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

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

module "graphql_content_review_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
  application_family                       = var.application_family
  aws_region                               = var.region
  commit_sha                               = "latest"
  container_port                           = "8080"
  task_type                                = "web_service"
  desired_task_count                       = 2
  task_cpu                                 = 2048
  task_memory                              = var.task_memory
  datadog_task_memory                      = var.datadog_memory
  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]

  fargate_service_subnets = module.vpc_info.default_private_subnet_ids

  load_balancer_subnets = module.vpc_info.default_private_subnet_ids

  iam_managed_policy_attachments = [
    data.aws_iam_policy.open_search_rw_policy.arn,
  ]

  environment_variables = [
    {
      NODE_OPTIONS = "--max-old-space-size=${var.task_memory - var.datadog_memory}"
    },
    {
      Environment = var.environment
    },
    {
      NODE_ENV = var.environment
    },
    {
      APOLLO_GRAPH_VARIANT = var.environment
    },
    {
      APOLLO_INTROSPECTION = var.introspection
    },
    {
      APOLLO_PLAYGROUND = var.playground
    },
    {
      DD_LOGS_INJECTION = true
    },
    {
      OPENSEARCH_URL = var.opensearch_url
    },
    {
      OPENSEARCH_INDEX_ALIAS = var.os_index_alias
    },
    {
      SENTRY_DSN = module.graphql_content_review_sentry_project.sentry_key_dsn_public_output
    }
  ]

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

module "graphql_content_review_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
  datadog_service_type              = "${var.service_name}-graphql"
  datadog_service_name              = "graphql.execute"
  application_family                = var.application_family
  service_4xx_monitor_enabled       = true
  healthy_tasks_evaluation_window   = "last_15m"
  notification_endpoints            = var.application_alert_endpoint
  escalation_notification_endpoints = var.application_escalation_endpoint
  additional_tags                   = { service_type = "critical" }
  dependent_applications            = var.dependent_applications
}
