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-user/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"]
}

module "graphql_user_sentry_module" {
  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 "elasticache" {
  # checkov:skip=CKV_AWS_30:ElastiCache encryption in transit requires client library update.
  # checkov:skip=CKV_AWS_31:ElastiCache encryption in transit requires client library update.
  source = "git@github.com:theorchard/terraform-elasticache.git//?ref=4.0.0"

  providers = {
    aws.dns = aws.networking
  }

  cache_transit_encryption_enabled = false

  environment                = "prod"
  service_name               = var.service_name
  application_family         = var.application_family
  redis_engine_version       = "5.0.6"
  cache_engine               = "redis"
  cache_subnet_group_name    = "prod-vpc-orchard-subnet"
  cache_node_type            = "cache.m6g.large"
  cache_parameter_group_name = "default.redis5.0"
  vpc_id                     = module.vpc_info.vpc_id
}

module "datadog_elasticache" {
  source = "git@github.com:theorchard/terraform-datadog.git//modules/elasticache?ref=6.15.3"

  environment        = var.environment
  service_name       = var.service_name
  application_family = var.application_family
  teams              = var.teams

  notification_endpoints            = var.application_alert_channel
  escalation_notification_endpoints = var.application_alert_channel
}

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_user_fargate_environment" {
  source = "git@github.com:theorchard/terraform-fargate.git//?ref=6.4.1"

  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
  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                     = true

  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 = "production"
    },
    {
      LOGGER_DSN = "https://${var.environment}-fluentd-applications.theorchard.io:8888/application"
    },
    {
      CACHE_USE_REDIS = "true"
    },
    {
      CACHE_REDIS_HOST = module.elasticache.redis_primary_endpoint_address
    },
    {
      APOLLO_INTROSPECTION = "false"
    },
    {
      APOLLO_PLAYGROUND = "false"
    },
    {
      DD_LOGS_INJECTION = true
    },
    {
      NEO4J_URL = var.neo4j_url
    },
    {
      ZENDESK_API_EMAIL = "zendesk-orchard-api@theorchard.com"
    },
    {
      ZENDESK_URL = "https://theorchardproduct.zendesk.com"
    },
  ]

  secrets = [
    {
      SENTRY_DSN = "${var.environment}/${var.service_name}/SENTRY_DSN"
    },
    {
      ZENDESK_API_TOKEN = "${var.environment}/${var.service_name}/ZENDESK_API_TOKEN"
    },
    {
      NEO4J_USER = "${var.environment}/${var.service_name}/NEO4J_USER"
    },
    {
      NEO4J_PASSWORD = "${var.environment}/${var.service_name}/NEO4J_PASSWORD"
    },
    {
      NEO4J_AURA_URL = "${var.environment}/${var.service_name}/NEO4J_AURA_URL"
    },
    {
      NEO4J_AURA_USERNAME = "${var.environment}/${var.service_name}/NEO4J_AURA_USERNAME"
    },
    {
      NEO4J_AURA_PASSWORD = "${var.environment}/${var.service_name}/NEO4J_AURA_PASSWORD"
    },
    {
      SPLIT_API_KEY = "${var.environment}/${var.service_name}/SPLIT_API_KEY"
    },
    {
      SEGMENT_WRITE_KEY = "${var.environment}/${var.service_name}/SEGMENT_WRITE_KEY"
    }
  ]
}

module "graphql_user_service_dashboard" {
  source                            = "git@github.com:theorchard/terraform-datadog.git//modules/service?ref=6.15.3"
  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
  critical_number_5xx               = var.critical_number_5xx
  critical_recovery_number_5xx      = var.critical_recovery_number_5xx
  warning_number_5xx                = var.warning_number_5xx
  warning_recovery_number_5xx       = var.warning_recovery_number_5xx
  notification_endpoints            = var.application_alert_channel
  escalation_notification_endpoints = var.application_alert_channel
  additional_tags                   = { service_type = "critical" }
  dependent_applications            = var.dependent_applications
}
