module "lambda_sr_delivery_tiktok_ack_processor_owsrequest" {
  source = "git@github.com:theorchard/terraform-owsrequest.git//?ref=1.1.0"

  environment_name = var.environment
  service_name     = var.sr_delivery_tiktok_ack_processor_name
}

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

  application_family       = var.application_family
  datadog_advanced_enabled = true
  environment              = var.environment
  lambda_name              = var.sr_delivery_tiktok_ack_processor_name
  lambda_description       = "Acknowledge processor for Tiktok Deliveries"
  lambda_function_timeout  = var.lambda_max_timeout
  use_container_image      = true
  vpc_id                   = module.vpc_info.vpc_id
  vpc_subnet_ids           = module.vpc_info.default_private_subnet_ids

  lambda_function_memory_size = 512

  lambda_function_environment_variables = {
    ENVIRONMENT               = var.environment
    SNOWFLAKE_ACCOUNT         = var.snowflake_account
    SNOWFLAKE_USER            = var.sr_ack_snowflake_user
    SNOWFLAKE_DATABASE        = var.snowflake_database
    SNOWFLAKE_SCHEMA          = var.snowflake_schema
    SNOWFLAKE_WAREHOUSE       = var.snowflake_warehouse
    SNOWFLAKE_TABLE           = var.snowflake_delivery_history_table
    SNOWFLAKE_ROLE            = var.snowflake_role
    LOGGER_LEVEL              = "INFO"
    LIMIT                    = "500"
    ACKS_FILTER_AWAITED_HOURS = "3300"
  }

  lambda_function_reserved_concurrent_executions = "3"

  iam_managed_policy_attachments = [
    module.lambda_sr_delivery_tiktok_ack_processor_owsrequest.policy_arn_output
  ]
}

module "tiktok_fingerprint_delivery_password" {
  source = "git@github.com:theorchard/terraform-secrets-manager.git//?ref=1.5.1"

  environment                    = var.environment
  service_name                   = var.sr_delivery_tiktok_ack_processor_name
  secret_name                    = "TIKTOK_FINGERPRINT_DELIVERY_PASSWORD"
  application_family             = var.application_family
  secret_recovery_window_in_days = 7
}

module "tiktok_fingerprint_delivery_user_name" {
  source = "git@github.com:theorchard/terraform-secrets-manager.git//?ref=1.5.1"

  environment                    = var.environment
  service_name                   = var.sr_delivery_tiktok_ack_processor_name
  secret_name                    = "TIKTOK_FINGERPRINT_DELIVERY_USER_NAME"
  application_family             = var.application_family
  secret_recovery_window_in_days = 7
}

module "lambda_sr_delivery_tiktok_ack_processor_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.sr_delivery_tiktok_ack_processor_name
  teams              = [var.environment]
}

module "datadog_lambda_sr_delivery_tiktok_ack_processor_dashboards" {
  source                                = "git@github.com:theorchard/terraform-datadog.git//modules/lambda?ref=6.13.7"
  service_name                          = var.sr_delivery_tiktok_ack_processor_name
  environment                           = var.environment
  lambda_invocation_monitor_enabled     = false
  lambda_error_monitor_enabled          = true
  lambda_error_critical_number          = 5
  lambda_error_critical_recovery_number = 3
  lambda_error_warning_number           = 2
  lambda_error_warning_recovery_number  = null
  notification_endpoints                = var.slack_alert_channel
}

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

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

moved {
  from = module.snowflake_secret
  to   = module.secrets["SNOWFLAKE_KEY"]
}
