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-account/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"
}

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

  environment        = var.environment
  platform           = "javascript"
  service_name       = var.service_name
  application_family = var.application_family
}

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
  application_family = var.application_family
}

module "graphql_account_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"
  datadog_task_memory                      = var.datadog_memory
  task_type                                = "web_service"
  desired_task_count                       = 4
  task_cpu                                 = 1024
  task_memory                              = var.task_memory
  maximum_capacity                         = 8
  minimum_capacity                         = 4
  scaling_cpu_target_value                 = 50
  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]

  # checkov:skip=ORCD_AWS_1:Silencing the "Avoid using subnets that are short on available IPs" notification.
  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 = "production"
    },
    {
      APOLLO_INTROSPECTION = "false"
    },
    {
      APOLLO_PLAYGROUND = "false"
    },
    {
      DD_LOGS_INJECTION = true
    },
    {
      SENTRY_DSN = module.graphql_account_sentry_project.sentry_key_dsn_public_output
    },
    {
      CACHE_REDIS_HOST = "rediss://${module.elasticache.redis_primary_endpoint_address}:6379"
    },
    {
      CACHE_USE_REDIS = false
    },
    {
      OPENSEARCH_URL = var.opensearch_url
    },
    {
      OPENSEARCH_ASSUME_ROLE_ARN = var.opensearch_assume_role_arn
    },
    {
      AWS_REGION = var.region
    }
  ]

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

module "graphql_account_service_dashboard" {
  source = "git@github.com:theorchard/terraform-datadog.git//modules/service?ref=6.13.4"

  environment  = var.environment
  service_name = var.service_name

  application_family                = var.application_family
  environment_type                  = "fargate"
  notification_endpoints            = "@slack-monitoring @slack-graphql @slack-accounts-team-alerts"
  datadog_service_type              = "${var.service_name}-graphql"
  datadog_service_name              = "graphql.execute"
  service_4xx_monitor_enabled       = false
  service_5xx_monitor_enabled       = true
  escalation_notification_endpoints = "@slack-monitoring @slack-graphql"
  dependent_applications            = var.dependent_applications
}

data "aws_iam_policy_document" "opensearch_assume_role_policy" {
  statement {
    effect    = "Allow"
    actions   = ["sts:AssumeRole"]
    resources = ["arn:aws:iam::${var.pp_account_id}:role/${var.environment}-cross-account-opensearch-role"]
  }
}

resource "aws_iam_policy" "opensearch_assume_role_policy" {
  name   = "${var.environment}-opensearch-assume-role-policy"
  policy = data.aws_iam_policy_document.opensearch_assume_role_policy.json
  tags   = local.tags
}

resource "aws_iam_role_policy_attachment" "opensearch_assume_role_policy_attachment" {
  role       = module.graphql_account_fargate_environment.fargate_task_iam_role_name
  policy_arn = aws_iam_policy.opensearch_assume_role_policy.arn
}

module "elasticache" {
  source = "git@github.com:theorchard/terraform-elasticache.git//?ref=3.1.1"

  providers = {
    aws.dns = aws.networking
  }

  environment                      = var.environment
  service_name                     = var.service_name
  application_family               = var.application_family
  cache_engine                     = "redis"
  redis_engine_version             = "7.1"
  cache_parameter_group_name       = "default.redis7"
  cache_subnet_group_name          = "${var.environment}-elasticache-subnet-group"
  cache_node_type                  = "cache.m6g.large"
  route53_zone_id                  = data.aws_route53_zone.route53_zone.zone_id
  vpc_id                           = module.vpc_info.vpc_id
  cache_transit_encryption_enabled = true
  cache_at_rest_encryption_enabled = true
}
module "datadog_elasticache" {
  source = "git@github.com:theorchard/terraform-datadog.git//modules/elasticache?ref=6.13.4"

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

  notification_endpoints            = var.notification_endpoints
  escalation_notification_endpoints = var.escalation_notification_endpoints
}
