locals {
  cleanup_lambda_name = "${var.service_name}-review-queue-cleanup"
}

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

  environment        = var.environment
  application_family = var.application_family
  lambda_name        = local.cleanup_lambda_name

  use_container_image      = true
  lambda_description       = "Remove deleted products from the review queue on a schedule"
  lambda_function_timeout  = "900"
  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

  cloudwatch_event_enabled  = var.review_queue_cleanup_cloudwatch_event_enabled
  cloudwatch_event_schedule = "rate(1 hour)"

  lambda_function_environment_variables = {
    ENVIRONMENT         = var.environment
    GRAPHQL_GATEWAY_URL = var.graphql_gateway_url
    SENTRY_DSN          = module.sentry_review_queue_cleanup.sentry_key_dsn_public_output
    SNOWFLAKE_ROLE      = var.snowflake_role
    SNOWFLAKE_WAREHOUSE = var.snowflake_warehouse
    SNOWFLAKE_USERNAME  = var.snowflake_username
    SNOWFLAKE_ACCOUNT   = "delphi.us-east-1"
  }

  # Added to fix datadog logging until ddtrace version can be upgraded
  datadog_advanced_merge_xray_traces_enabled = false
}

# stage datadog creds and create dashboards.
module "datadog_lambda_content_review_queue_cleanup" {
  source = "git@github.com:theorchard/terraform-datadog.git//modules/lambda?ref=6.13.9"

  environment                       = var.environment
  service_name                      = local.cleanup_lambda_name
  lambda_invocation_monitor_enabled = false
  notification_endpoints            = var.notification_endpoints
  escalation_notification_endpoints = var.escalation_notification_endpoints
}

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

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

module "review-queue-cleanup-lambda-secrets" {
  source   = "git@github.com:theorchard/terraform-secrets-manager.git//?ref=1.5.1"
  for_each = toset(var.cleanup_secrets_manager_secret_names)

  environment                    = var.environment
  service_name                   = local.cleanup_lambda_name
  secret_name                    = each.value
  application_family             = var.application_family
  secret_recovery_window_in_days = 7
}
