module "default_tags" {
  source             = "git@github.com:theorchard/terraform-default-tags.git//?ref=1.0.0"
  environment        = var.environment
  application_family = var.application_family
  service_name       = var.service_name
}

provider "aws" {
  region = var.aws_region

  default_tags {
    tags = module.default_tags.tags
  }
}

provider "aws" {
  region  = var.aws_region
  alias   = "networking"
  profile = "networking"

  default_tags {
    tags = module.default_tags.tags
  }
}

# Terraform backends cannot contain interpolations
terraform {
  backend "s3" {
    bucket  = "orcd-terraform-state"
    key     = "qa/ows-masters-celery/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

module "vpc_info" {
  source = "git@github.com:theorchard/terraform-vpc-info.git?ref=3.0.2"

  environment = var.environment
}

data "aws_acm_certificate" "theorchard_io" {
  domain   = "*.theorchard.io"
  statuses = ["ISSUED"]
}

data "aws_route53_zone" "route53_zone" {
  name = "theorchard.io"
}

data "aws_elasticache_replication_group" "ows_masters_registry" {
  replication_group_id = "${var.environment}-ows-masters-registry"
}

module "ows_masters_celery_owsrequest" {
  source           = "git@github.com:theorchard/terraform-owsrequest.git//?ref=1.0.1"
  environment_name = var.environment
  service_name     = var.service_name
}

module "ows_masters_celery_fargate_environment" {
  source = "git@github.com:theorchard/terraform-fargate.git//?ref=5.5.1"

  providers = {
    aws.dns = aws.networking
  }

  environment                              = var.environment
  service_name                             = var.service_name
  application_family                       = var.application_family
  aws_region                               = var.aws_region
  commit_sha                               = "latest"
  container_port                           = "5555"
  task_type                                = "web_service"
  desired_task_count                       = 1
  task_cpu                                 = 1024
  task_memory                              = 2048
  maximum_capacity                         = 3
  minimum_capacity                         = 1
  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]
  health_check_path                        = "/"
  blocking_waf_enabled                     = false
  splitio_enabled                          = false

  iam_managed_policy_attachments = [
    "arn:aws:iam::437795906767:policy/SecretsManager-qa-ows-masters-registry-policy"
  ]

  fargate_service_subnets = module.vpc_info.default_private_subnet_ids

  load_balancer_subnets = module.vpc_info.default_private_subnet_ids

  environment_variables = [
    {
      Environment = var.environment
    },
    {
      AWS_DEFAULT_REGION = "us-east-1"
    },
    {
      BROKER_URL = "redis://:@${data.aws_elasticache_replication_group.ows_masters_registry.primary_endpoint_address}:6379"
    },
    {
      CELERY_LOGGER_LEVEL = "20"
    },
    {
      CELERY_RESULT_BACKEND = "redis://:@${data.aws_elasticache_replication_group.ows_masters_registry.primary_endpoint_address}:6379"
    },
    {
      CHECK_CARVEOUTS_ON_FIRST_IMPORT = "1"
    },
    {
      NOT_ADDED_TERRITORIES_WARNING = "1"
    },
    {
      PREFETCH_MULTIPLIER = "4"
    },
    {
      SAVE_BULK_TASK_CONTEXT = "1"
    },
    {
      SHOW_LOCK_HISTORY = "1"
    },
    {
      SHOW_SUB_STORE_CARVED_OUT = "1"
    },
    {
      TASK_ACKS_LATE = "0"
    },
    {
      UPC_MUSIC_VIDEO_VALIDATION = "1"
    }
  ]
  secrets = [
    {
      AWS_ACCESS_KEY_ID = "${var.environment}/${var.service_name}/AWS_ACCESS_KEY_ID"
    },
    {
      AWS_SECRET_ACCESS_KEY = "${var.environment}/${var.service_name}/AWS_SECRET_ACCESS_KEY"
    }
  ]
}

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