module "ecs_abacus_extract_sales_secrets" {
  source   = "git@github.com:theorchard/terraform-secrets-manager.git//?ref=1.5.1"
  for_each = toset(var.secrets_manager_secret_names_for_extract_sales)

  application_family = var.application_family
  environment        = var.environment
  service_name       = "${var.service_name}-extract-sales"
  secret_name        = each.value
}

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

  providers = {
    aws.dns = aws
  }

  environment        = var.environment
  service_name       = "${var.service_name}-extract-sales"
  aws_region         = var.aws_region
  application_family = var.application_family

  task_type                          = "worker"
  health_check_command               = "pgrep python"
  deployment_minimum_healthy_percent = "100"
  autoscaling_cpu_policy_enabled     = false
  autoscaling_memory_policy_enabled  = false
  desired_task_count                 = 0
  minimum_capacity                   = 0
  maximum_capacity                   = 1
  task_cpu                           = 1024
  task_memory                        = 4096
  container_port                     = 2048
  health_check_grace_period_seconds  = 300
  container_start_period_seconds     = 300
  target_deregistration_delay        = 300

  datadog_enabled = true

  vpc_id                  = module.vpc_info.vpc_id
  fargate_service_subnets = module.vpc_info.default_private_subnet_ids

  environment_variables = [
    {
      Environment = var.environment
    },
    {
      SSH_HOST = var.statement_db_ssh_host
    },
    {
      SSH_USER = var.statement_db_ssh_user
    },
    {
      MYSQL_DB_USER = var.statement_db_user
    },
    {
      S3_BUCKET_NAME = var.sales_file_bucket_name
    },
    {
      OUTFILE_DIR = var.outfile_dir
    },
    {
      SERVICE_USER = var.service_user
    },
    {
      SERVICE_GROUP = var.service_group
    },
    {
      SPLIT_SIZE = var.split_size
  }]
}

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

  application_family = var.application_family
  environment        = var.environment
  platform           = "python"
  service_name       = "${var.service_name}-extract-sales"
  teams              = ["abacus-${var.environment}"]
}

module "extract_sales_fargate_service_dashboard" {
  source             = "git@github.com:theorchard/terraform-datadog.git//modules/service//?ref=6.10.7"
  environment        = var.environment
  environment_type   = "fargate"
  application_family = var.application_family
  service_name       = "${var.service_name}-extract-sales"

  healthy_tasks_monitor_enabled = false
  service_4xx_monitor_enabled   = false
  service_5xx_monitor_enabled   = false

  notification_endpoints            = var.notification_endpoints
  escalation_notification_endpoints = var.notification_endpoints
}
