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.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-router/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

data "aws_caller_identity" "current" {}

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_ec2_managed_prefix_list" "fansifter_prefix_list" {
  name = "${var.environment}-fansifter-private-subnet-prefix-list"
}

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

  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                       = 4
  task_cpu                                 = 2048
  task_memory                              = 16384
  maximum_capacity                         = 16
  minimum_capacity                         = 4
  scaling_cpu_target_value                 = 50
  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]
  blocking_waf_enabled                     = false
  health_check_path                        = "/health"

  fargate_service_subnets = module.vpc_info.default_private_subnet_ids

  load_balancer_subnets = module.vpc_info.default_private_subnet_ids

  https_listener_allow_prefix_list_names = [
    "shared-orcd-jump-box-private-subnet-prefix-list",
  ]

  environment_variables = [
    {
      Environment = var.environment
    },
    {
      CONFIG_FILE = "config-${var.environment}.yaml"
    },
    {
      NODE_ENV = var.environment
    },
    {
      INTROSPECTION = var.introspection
    },
    {
      TRACING = var.tracing
    },
    {
      DD_LOGS_INJECTION = true
    },
    {
      APOLLO_GRAPH_REF = "graphql-theorchard@${var.environment}"
    }
  ]

  secrets = [
    {
      APOLLO_KEY = "${var.environment}/${var.service_name}/APOLLO_ENGINE_API_KEY"
    }
  ]
}

module "secrets" {
  source   = "git@github.com:theorchard/terraform-secrets-manager.git//?ref=1.5.1"
  for_each = toset(var.secrets_manager_secret_names)

  environment                    = var.environment
  service_name                   = var.service_name
  secret_name                    = each.value
  application_family             = var.application_family
  secret_recovery_window_in_days = 7
}

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

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

  environment        = var.environment
  service_name       = var.service_name
  application_family = var.application_family
}

# Allow services in fansifter-prod AWS account
# interact with prod-graphql-router
resource "aws_security_group_rule" "allow_fansifter_ingress" {
  type              = "ingress"
  from_port         = 443
  to_port           = 443
  protocol          = "TCP"
  security_group_id = module.graphql_router_fargate_environment.fargate_load_balancer_security_group_id
  prefix_list_ids = [
    data.aws_ec2_managed_prefix_list.fansifter_prefix_list.id
  ]
  depends_on = [module.graphql_router_fargate_environment]
}
