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-product/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."
}

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=3.1.1"

  providers = {
    aws.dns = aws.networking
  }

  cache_transit_encryption_enabled = false

  environment                    = "prod"
  service_name                   = var.service_name
  redis_engine_version           = "5.0.6"
  cache_engine                   = "redis"
  cache_subnet_group_name        = "prod-vpc-orchard-subnet"
  cache_node_type                = "cache.t4g.micro"
  cache_node_count               = 1
  cache_parameter_group_name     = "default.redis5.0"
  vpc_id                         = module.vpc_info.vpc_id
  route53_zone_id                = data.aws_route53_zone.route53_zone.zone_id
  additional_cidr_blocks_enabled = true
  application_family             = "product-builder"
  additional_cidr_blocks = [
    "10.40.0.0/22",
    "10.30.0.0/22",
    "192.168.31.0/24",
    "192.168.32.0/24",
    "192.168.33.0/24",
    "192.168.40.0/22",
  ]
}

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

  providers = {
    aws.dns = aws.networking
  }

  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                                  = 4
  task_cpu                                            = 2048
  task_memory                                         = var.task_memory
  datadog_task_memory                                 = var.datadog_memory
  maximum_capacity                                    = 16
  minimum_capacity                                    = 4
  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]
  autoscaling_cpu_policy_enabled                      = false
  autoscaling_active_connections_policy_enabled       = true
  scaling_active_connection_count_scale_out_threshold = 15
  scaling_active_connection_count_scale_in_threshold  = 5
  application_family                                  = var.application_family

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

  # overriding here to add in the graphql_switchboard_vpc (10.120.0.0/16) private subnets
  prod_https_listener_allow_cidr_blocks = [
    "192.168.31.0/24",
    "10.40.0.0/22",
    "10.30.0.0/22",
    "10.10.40.0/22",
    "10.120.0.0/24",
    "10.120.1.0/24",
  ]

  fargate_service_subnets = ["subnet-098b89d0c58c5693a", "subnet-067a6b45b079d7296", "subnet-043d235081c966332"]

  load_balancer_subnets = ["subnet-098b89d0c58c5693a", "subnet-067a6b45b079d7296", "subnet-043d235081c966332"]

  environment_variables = [
    {
      NODE_OPTIONS = "--max-old-space-size=${var.task_memory - var.datadog_memory - var.fluentbit_memory - 128}"
    },
    {
      Environment = var.environment
    },
    {
      NODE_ENV = var.environment
    },
    {
      APOLLO_ENGINE_SCHEMA_TAG = var.environment
    },
    {
      LOGGER_DSN = "https://${var.environment}-fluentd-applications.theorchard.io:8888/application"
    },
    {
      CACHE_USE_REDIS = "true"
    },
    {
      CACHE_REDIS_HOST = module.elasticache.redis_primary_endpoint_address
    },
    {
      PLAYGROUND = var.playground
    },
    {
      TRACING = var.tracing
    },
    {
      CORS = var.cors
    },
    {
      DEBUG = var.debug
    },
    {
      DD_LOGS_INJECTION = true
    },
    {
      LOG_LEVEL = "info"
    },
    {
      SPLIT_REDIS_URL = "prod-split-synchronizer.oudhyr.ng.0001.use1.cache.amazonaws.com:6379"
    },
    {
      AUTH0_DOMAIN = "workstation.auth0.com"
    },
    {
      INTROSPECTION = var.introspection
    },
    {
      CONTENT_OPENSEARCH_URL = var.content_opensearch_url
    },
    {
      CONTENT_OPENSEARCH_USERNAME = var.content_opensearch_username
    },
  ]
  secrets = [
    {
      CONTENT_OPENSEARCH_PASSWORD = "${var.environment}/${var.service_name}/CONTENT_OPENSEARCH_PASSWORD"
    },
    {
      CHARTMETRIC_REFRESH_TOKEN = "${var.environment}/${var.service_name}/CHARTMETRIC_REFRESH_TOKEN"
    },
    {
      SENTRY_DSN = "${var.environment}/${var.service_name}/SENTRY_DSN"
    },
    {
      APOLLO_ENGINE_API_KEY = "${var.environment}/${var.service_name}/APOLLO_ENGINE_API_KEY"
    },
    {
      FACEBOOK_CLIENT_ID = "${var.environment}/${var.service_name}/FACEBOOK_CLIENT_ID"
    },
    {
      SPLIT_API_KEY = "${var.environment}/${var.service_name}/SPLIT_API_KEY"
    },
    {
      ACTIVITY_FEED_API_KEY = "${var.environment}/${var.service_name}/ACTIVITY_FEED_API_KEY"
    },
  ]
}

module "graphql_product_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
  critical_number_5xx               = var.critical_number_5xx
  critical_recovery_number_5xx      = var.critical_recovery_number_5xx
  datadog_service_type              = "${var.service_name}-graphql"
  datadog_service_name              = "graphql.execute"
  warning_number_5xx                = var.warning_number_5xx
  warning_recovery_number_5xx       = var.warning_recovery_number_5xx
  notification_endpoints            = var.notification_endpoints
  escalation_notification_endpoints = var.escalation_notification_endpoints
  additional_tags                   = { service_type : "critical" }
  teams                             = [var.application_family]
  service_4xx_monitor_enabled       = true
  dependent_applications            = var.dependent_applications
}

module "multiple_secrets_with_same_settings" {
  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
  # temporary switch to KDH app family so we can edit the secrets with real values.
  application_family             = var.kdh_application_family
  secret_recovery_window_in_days = 7
}
