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 "code_push_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   = 8
  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.prod_mobile_code_push_pdestorage_com.arn)[1]

  environment_variables = [
    {
      AWS_REGION = var.aws_region
    },
    {
      NODE_ENV = var.environment
    },
    {
      AWS_BUCKET_NAME = module.s3_code_push_server_bucket.s3_bucket_name_output
    },
    {
      REDIS_HOST = module.code_push_server_cache.redis_primary_endpoint_address
    },
    {
      REDIS_PORT = 6379
    },
    {
      SERVER_URL = "https://${var.environment}-${var.service_name}.${var.route53_hosted_zone}"
    },
    {
      CLOUDFRONT_URL = "https://${var.environment}-${var.service_name}-cdn.${var.route53_hosted_zone}"
    },
    {
      CORS_ORIGIN = join(",", [
        "https://${var.service_name}.${var.route53_hosted_zone}",
        "https://${var.environment}-${var.service_name}.${var.route53_hosted_zone}",
        "https://${var.environment}-${var.service_name}-public.${var.route53_hosted_zone}",
        "https://${var.service_name}-public.${var.route53_hosted_zone}",
      ])
    },
    {
      TMPDIR = "/tmp"
    },
    {
      STORAGE_TYPE = "PostgresS3Storage"
    },
    {
      REQUEST_TIMEOUT_IN_MILLISECONDS = "59000"
    },
    {
      PG_HOST = module.code_push_server_rds.rds_cluster_endpoint
    },
    {
      PG_DATABASE = module.code_push_server_rds.rds_cluster_database_name
    },
    {
      PG_PORT = module.code_push_server_rds.rds_cluster_port
    },
    {
      PG_USER = "code_push_server_user"
    },
    {
      PG_SSL = "true"
    },
    {
      MICROSOFT_TENANT_ID = "f0aff3b7-91a5-4aae-af71-c63e1dda2049"
    },
    {
      LOGGING = "true"
    },
  ]

  secrets = [
    {
      GITHUB_CLIENT_ID = "${var.environment}/${var.service_name}/GITHUB_CLIENT_ID"
    },
    {
      GITHUB_CLIENT_SECRET = "${var.environment}/${var.service_name}/GITHUB_CLIENT_SECRET"
    },
    {
      MICROSOFT_CLIENT_ID = "${var.environment}/${var.service_name}/MICROSOFT_CLIENT_ID"
    },
    {
      MICROSOFT_CLIENT_SECRET = "${var.environment}/${var.service_name}/MICROSOFT_CLIENT_SECRET"
    },
    {
      SENTRY_DSN = "${var.environment}/${var.service_name}/SENTRY_DSN"
    },
    {
      PG_PASSWORD = "${var.environment}/${var.service_name}/PG_PASSWORD"
    },
    {
      DATADOG_API_KEY = "${var.environment}/datadog/DD_API_KEY"
    },
  ]

  iam_managed_policy_attachments = [
    aws_iam_policy.s3_code_push_server_bucket_read_write_policy.arn,
  ]

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

module "code_push_serve_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 "code_push_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
}
