locals {
  lambda_store_product_metadata = "${var.service_name}-store-product-metadata"
  store_product_queue           = "content-review-store-product"
}

data "aws_iam_policy_document" "store_product_metadata_sfn_policy" {
  statement {
    actions = [
      "states:StartExecution"
    ]
    resources = [
      aws_sfn_state_machine.sfn_content_auto_approve.arn
    ]
  }
}

resource "aws_iam_policy" "store_product_metadata_sfn_policy" {
  name   = "${var.environment}-${local.lambda_store_product_metadata}-sfn-invoke"
  policy = data.aws_iam_policy_document.store_product_metadata_sfn_policy.json
}

module "store-product-queue" {
  source = "git@github.com:theorchard/terraform-sqs.git?ref=2.4.1"

  environment                     = var.environment
  queue_name                      = local.store_product_queue
  application_family              = var.application_family
  sqs_fifo_queue                  = false
  sqs_delay_seconds               = 10
  sqs_max_message_size            = 2048 # 2 KB
  sqs_message_retention_seconds   = 86400 # 1 day
  sqs_receive_wait_time_seconds   = 10
  sqs_visibility_timeout_seconds  = 5400 # minimum 6x lambda function timeout to allow for retries

  sqs_deadletter_enabled = true

  deadletter_max_receive_count         = 5 # Fail the message after 5 receives
  deadletter_delay_seconds             = 10
  deadletter_max_message_size          = 2048 # 2 KB
  deadletter_message_retention_seconds = 86400 # 1 day
  deadletter_receive_wait_time_seconds = 10
}

data "aws_iam_policy" "store_product_metadata_rw_policy" {
  name = "S3-${var.environment}-${var.bucket_name}-RW"
}

data "aws_iam_policy" "store_product_sqs_policy" {
  name = "SQS-${var.environment}-${local.store_product_queue}"
}

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

  environment        = var.environment
  application_family = var.application_family
  lambda_name        = local.lambda_store_product_metadata

  use_container_image                            = true
  lambda_description                             = "Fetches and stores product metadata snapshots to S3 for the content auto-approve workflow."
  lambda_function_timeout                        = "900"
  lambda_function_reserved_concurrent_executions = var.store_product_metadata_lambda_reserved_concurrency
  vpc_enabled                                    = true
  vpc_id                                         = var.vpc_id
  vpc_subnet_ids                                 = var.vpc_subnet_ids
  datadog_enabled                                = true
  datadog_advanced_enabled                       = true

  iam_managed_policy_attachments = [
    data.aws_iam_policy.store_product_metadata_rw_policy.arn,
    data.aws_iam_policy.store_product_sqs_policy.arn,
    aws_iam_policy.store_product_metadata_sfn_policy.arn
  ]

  lambda_function_environment_variables = {
    ENVIRONMENT                 = var.environment
    SYSTEM_USER_IDENTITY_ID     = "071abc96-4172-4c1a-89a2-842fb994f705"
    SYSTEM_USER_PROFILE_ID      = "665891"
    SYSTEM_USER_PROFILE_TYPE    = "ContentProfile"
    AUTO_APPROVE_STEP_FUNC_ARN  = aws_sfn_state_machine.sfn_content_auto_approve.arn
  }

  datadog_advanced_merge_xray_traces_enabled = false
}

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

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

  lambda_error_warning_number          = 1
  lambda_error_warning_recovery_number = 0
}

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

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

resource "aws_lambda_event_source_mapping" "store_product_metadata_sqs_event_source" {
  event_source_arn                    = module.store-product-queue.queue_arn
  function_name                       = module.lambda_content_store_product_metadata.lambda_arn
  batch_size                          = var.store_product_metadata_sqs_batch_size
  maximum_batching_window_in_seconds  = var.store_product_metadata_sqs_batch_window
  enabled                             = true
  function_response_types             = ["ReportBatchItemFailures"]
}
