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-participant/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 "graphql_participant_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
}

data "aws_iam_policy" "ows_machine_to_machine_policy" {
  name = "SecretsManager-${var.environment}-ows-machine-to-machine-policy"
}

module "graphql_participant_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
  aws_region                                          = var.region
  commit_sha                                          = "latest"
  container_port                                      = "8080"
  task_type                                           = "web_service"
  desired_task_count                                  = 8
  task_cpu                                            = 1024
  task_memory                                         = 2048
  maximum_capacity                                    = 12
  minimum_capacity                                    = 2
  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]
  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
  fargate_service_subnets                             = module.vpc_info.default_private_subnet_ids
  load_balancer_subnets                               = module.vpc_info.default_private_subnet_ids

  iam_managed_policy_attachments = [
    aws_iam_policy.isni_bucket_rw_policy.arn,
    data.aws_iam_policy.ows_machine_to_machine_policy.arn,
  ]

  environment_variables = [
    {
      Environment = var.environment
    },
    {
      NODE_ENV = var.environment
    },
    {
      LOGGER_DSN = "https://${var.environment}-fluentd-applications.theorchard.io:8888/application"
    },
    {
      APOLLO_INTROSPECTION = var.introspection
    },
    {
      APOLLO_PLAYGROUND = var.playground
    },
    {
      DEBUG = var.debug
    },
    {
      DD_LOGS_INJECTION = true
    },
    {
      NEO4J_URL = var.neo4j_url
    },
    {
      NEO4J_AURA_URL = var.neo4j_aura_url
    },
  ]

  secrets = [
    {
      SENTRY_DSN = "${var.environment}/${var.service_name}/SENTRY_DSN"
    },
    {
      NEO4J_USER = "${var.environment}/${var.service_name}/NEO4J_USERNAME"
    },
    {
      NEO4J_PASSWORD = "${var.environment}/${var.service_name}/NEO4J_PASSWORD"
    },
    {
      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 = "prod/split/API_KEY"
    },
    {
      QUANSIC_API_KEY = "${var.environment}/${var.service_name}/QUANSIC_API_KEY"
    }
  ]
}

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

  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

  healthy_tasks_notification_overrides = {
    endpoints            = var.healthy_tasks_notification_endpoints
    escalation_endpoints = var.healthy_tasks_notification_endpoints
  }
}

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
}
