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

  providers = {
    aws.dns = aws
  }

  environment        = var.environment
  service_name       = var.service_name
  application_family = var.application_family
  aws_region         = var.aws_region
  commit_sha         = "latest"
  container_port     = "3000"
  task_type          = "web_service"
  desired_task_count = 2
  task_cpu           = 1024
  task_memory        = 2048
  maximum_capacity   = 4
  minimum_capacity   = 2
  health_check_path  = "/health"

  custom_waf_arn = module.custom_waf.waf_blocking_arn_output

  # additional_lb_target_group_arns = [aws_lb_target_group.public_fargate_target_group.arn]

  vpc_id                        = module.vpc_info.vpc_id
  fargate_service_subnets       = module.vpc_info.default_private_subnet_ids
  load_balancer_subnets         = module.vpc_info.default_private_subnet_ids
  route53_zone_id               = data.aws_route53_zone.route53_zone.zone_id
  https_listener_certificate_id = split("/", data.aws_acm_certificate.qa_mobile_hot_updater_pdestorage_com.arn)[1]

  environment_variables = [
    {
      NODE_ENV = var.environment
    },
    {
      API_PORT = var.container_port
    },
    {
      PG_HOST = module.hot_updater_server_rds.rds_cluster_endpoint
    },
    {
      PG_PORT = module.hot_updater_server_rds.rds_cluster_port
    },
    {
      PG_DATABASE = module.hot_updater_server_rds.rds_cluster_database_name
    },
    {
      PG_USER = "hot_updater_server_user"
    },
    {
      PG_SSL = "true"
    },
    {
      AWS_ENDPOINT = "https://s3.${var.aws_region}.amazonaws.com"
    },
    {
      AWS_REGION = var.aws_region
    },
    {
      AWS_BUCKET = module.s3_hot_updater_server_bucket.s3_bucket_name_output
    },
    {
      USE_LOCALSTACK = "false"
    },
    {
      CDN_HOST = "https://${var.environment}-${var.service_name}-cdn.${var.route53_hosted_zone}"
    },
    {
      SENTRY_DSN = module.hot_updater_server_sentry_project.sentry_key_dsn_public_output
    },
    {
      DD_SITE = "datadoghq.com"
    },
    {
      DD_FLUSH_INTERVAL = "60"
    }
  ]

  secrets = [
    {
      API_MASTER_KEY = "${var.environment}/${var.service_name}/API_MASTER_KEY"
    },
    {
      DD_API_KEY = "${var.environment}/datadog/DD_API_KEY"
    },
    {
      PG_PASSWORD = "${var.environment}/${var.service_name}/PG_PASSWORD"
    }
  ]

  iam_managed_policy_attachments = [
    aws_iam_policy.s3_hot_updater_server_bucket_read_write_policy.arn,
  ]

  https_listener_allow_prefix_list_names = [
    "shared-orcd-private-subnet-prefix-list",
  ]
}

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

  environment        = var.environment
  platform           = "node-express"
  service_name       = var.service_name
  teams              = [var.environment]
  application_family = var.application_family
}

module "hot_updater_server_fargate_dashboard" {
  source                            = "git@github.com:theorchard/terraform-datadog.git//modules/service?ref=6.13.4"
  environment                       = var.environment
  application_family                = var.application_family
  environment_type                  = "fargate"
  service_name                      = var.service_name
  service_4xx_monitor_enabled       = false
  notification_endpoints            = var.notification_endpoints
  escalation_notification_endpoints = var.escalation_notification_endpoints
}
