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 {
  backend "s3" {
    bucket  = "orcd-terraform-state"
    key     = "prod/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.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"
}

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.1.0"
  environment_name = var.environment
  service_name     = var.service_name
}

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

  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   = 1
  minimum_capacity   = 1

  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

  # Overriding here to add in the the ny internal and vpn private subnets to monitor celery bulk status by distro team.
  prod_https_listener_allow_cidr_blocks = [
    "192.168.31.0/24",
    "10.40.0.0/22",
    "10.10.40.0/22",
  ]

  https_listener_allow_prefix_list_names = ["vpn-ny-users"]

  iam_managed_policy_attachments = [
    "arn:aws:iam::437795906767:policy/SecretsManager-prod-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.17.1"
  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
  service_cpu_monitor_enabled       = false

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