locals {
  update_indexed_products_lambda_name = "${var.service_name}-update-indexed-products"
}

module "lambda_content_update_indexed_products" {
  source = "git@github.com:theorchard/terraform-lambda.git//?ref=4.3.0"

  environment        = var.environment
  application_family = var.application_family
  lambda_name        = local.update_indexed_products_lambda_name

  use_container_image                            = true
  lambda_description                             = "Updates products indexed in opensearch."
  lambda_function_timeout                        = "900"
  lambda_function_reserved_concurrent_executions = 10
  vpc_enabled                                    = true
  vpc_id                                         = var.vpc_id
  vpc_subnet_ids                                 = var.vpc_subnet_ids
  datadog_enabled                                = true
  datadog_advanced_enabled                       = true
  additional_tags                                = var.additional_tags

  sqs_event_enabled                  = true
  event_source_mapping_resource_type = "sqs"
  event_source_mapping_queue_name    = "${var.environment}-content-review-index-update-queue"
  event_source_mapping_batch_size    = 1

  iam_managed_policy_attachments = [
    data.aws_iam_policy.opensearch_rw_policy.arn
  ]

  lambda_function_environment_variables = {
    ENVIRONMENT              = var.environment
    SENTRY_DSN               = module.sentry_lambda_content_update_indexed_products.sentry_key_dsn_public_output
    SYSTEM_USER_IDENTITY_ID  = "73524c1c-453e-4a3f-9b82-b5fd170534c3"
    SYSTEM_USER_PROFILE_ID   = "660963"
    SYSTEM_USER_PROFILE_TYPE = "ContentProfile"
    OPENSEARCH_ENDPOINT    = var.os_domain_endpoint
    OPENSEARCH_PORT        = var.os_domain_port
    OPENSEARCH_INDEX_ALIAS = var.opensearch_index_write_alias
  }
}

module "datadog_lambda_content_update_indexed_products" {
  source = "git@github.com:theorchard/terraform-datadog.git//modules/lambda?ref=6.13.9"

  environment                       = var.environment
  service_name                      = local.update_indexed_products_lambda_name
  notification_endpoints            = var.notification_endpoints
  escalation_notification_endpoints = var.notification_endpoints

  lambda_error_warning_number           = 2500
  lambda_error_warning_recovery_number  = 2499
  lambda_error_critical_number          = 5000
  lambda_error_critical_recovery_number = 4999
}

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

  environment        = var.environment
  service_name       = local.update_indexed_products_lambda_name
  application_family = var.application_family
}
