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

module "elasticache" {
  # checkov:skip=CKV_AWS_30:explicitly disable due to cache_transit_encryption_enabled explicitly false.
  # checkov:skip=CKV_AWS_31:explicitly disable due to cache_transit_encryption_enabled explicitly false.
  source = "git@github.com:theorchard/terraform-elasticache.git//?ref=4.0.0"

  providers = {
    aws.dns = aws.networking
  }

  cache_transit_encryption_enabled = false

  environment                    = "qa"
  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.t4g.micro"
  cache_parameter_group_name     = "default.redis5.0"
  vpc_id                         = module.vpc_info.vpc_id
  additional_cidr_blocks_enabled = true
  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",
  ]
}

data "aws_iam_policy" "opensearch_rw_policy" {
  name = "Elasticsearch-qa-content-search-RW-policy"
}

module "graphql_product_fargate_environment" {
  source = "git@github.com:theorchard/terraform-fargate.git//?ref=6.3.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                                      = 1024
  task_memory                                   = var.task_memory
  datadog_task_memory                           = var.datadog_memory
  maximum_capacity                              = 8
  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]
  http_listener_enabled                         = false
  autoscaling_cpu_policy_enabled                = false
  autoscaling_active_connections_policy_enabled = true
  stopped_task_monitoring_enabled               = true
  https_listener_allow_prefix_list_names = [
    "uat-orcd-private-subnet-prefix-list",
  ]
  iam_managed_policy_attachments = [
    data.aws_iam_policy.opensearch_rw_policy.arn,
  ]

  # overriding here to add in the graphql_switchboard_vpc (10.110.0.0/16) private subnets
  qa_https_listener_allow_cidr_blocks = [
    "192.168.31.0/24",
    "192.168.32.0/24",
    "192.168.33.0/24",
    "192.168.40.0/22",
    "10.30.0.0/22",
    "10.40.0.0/22",
    "10.110.0.0/24",
    "10.110.1.0/24",
  ]

  fargate_service_subnets = module.vpc_info.default_private_subnet_ids

  load_balancer_subnets = module.vpc_info.default_private_subnet_ids

  environment_variables = [
    {
      NODE_OPTIONS = "--max-old-space-size=${var.task_memory - var.datadog_memory}"
    },
    {
      Environment = var.environment
    },
    {
      NODE_ENV = 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
    },
    {
      APOLLO_PLAYGROUND = "true"
    },
    {
      APOLLO_INTROSPECTION = "true"
    },
    {
      SPLIT_REDIS_URL = "qa-split-synchronizer.oudhyr.ng.0001.use1.cache.amazonaws.com:6379"
    },
    {
      AUTH0_DOMAIN = "qa-orchard.auth0.com"
    },
    {
      DD_LOGS_INJECTION = true
    },
    {
      OWS_REQUEST_TRACING = true
    },
    {
      CONTENT_OPENSEARCH_URL = var.content_opensearch_url
    },
    {
      CONTENT_OPENSEARCH_USERNAME = var.content_opensearch_username
    },
    {
      SENTRY_DSN = "https://df65084b14c445f8a1aa1250d2df5bc4@o22178.ingest.us.sentry.io/1338164"
    },
  ]
  secrets = [
    {
      CONTENT_OPENSEARCH_PASSWORD = "${var.environment}/${var.service_name}/CONTENT_OPENSEARCH_PASSWORD"
    },
    {
      CHARTMETRIC_REFRESH_TOKEN = "${var.environment}/${var.service_name}/CHARTMETRIC_REFRESH_TOKEN"
    },
    {
      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.13.6"
  environment                       = var.environment
  environment_type                  = "fargate"
  service_name                      = var.service_name
  application_family                = var.application_family
  notification_endpoints            = "@slack-monitoring @slack-graphql"
  escalation_notification_endpoints = "@slack-monitoring @slack-graphql"
  datadog_service_type              = "${var.service_name}-graphql"
  datadog_service_name              = "graphql.execute"
  service_4xx_monitor_enabled       = true
  service_5xx_monitor_enabled       = true
}

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

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

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

  # KDH-589 temporary switch to KDH app family so we can edit the secrets with real values.
  application_family = var.kdh_application_family
  environment        = var.environment
  service_name       = var.service_name
  secret_name        = each.value
}
