locals {
  lambda_initiate_indexing = "${var.service_name}-initiate-indexing"
  sqs_store_product        = "${var.environment}-content-review-store-product"
}

data "aws_sfn_state_machine" "sfn_content_index_product_metadata" {
  name = "${var.environment}-${local.sfn_name}"
}

data "aws_sfn_state_machine" "sfn_content_auto_approve" {
  name = "${var.environment}-sfn-content-auto-approve"
}


data "aws_iam_policy_document" "start_sfn_policy_document" {
  statement {
    actions = [
      "states:StartExecution"
    ]

    resources = [
      data.aws_sfn_state_machine.sfn_content_index_product_metadata.arn,
      data.aws_sfn_state_machine.sfn_content_auto_approve.arn
    ]
  }
}

data "aws_sqs_queue" "store_product_queue" {
  name = "${local.sqs_store_product}-queue"
}

data "aws_iam_policy" "store_product_sqs_policy" {
  name = "SQS-${local.sqs_store_product}"
}

resource "aws_iam_policy" "start_sfn_policy" {
  name        = "StepFunctions-${var.environment}-${local.sfn_name}-StartExecution"
  policy      = data.aws_iam_policy_document.start_sfn_policy_document.json
  description = "Policy that allows invoking the content index product metadata sfn."
}

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

  environment        = var.environment
  application_family = var.application_family
  lambda_name        = local.lambda_initiate_indexing

  use_container_image                            = true
  lambda_description                             = "React to updates to content_review.review_queue and trigger index product metadata step function."
  lambda_function_timeout                        = "600"
  lambda_function_reserved_concurrent_executions = var.initiate_indexing_concurrency
  vpc_enabled                                    = true
  vpc_id                                         = var.vpc_id
  vpc_subnet_ids                                 = var.vpc_subnet_ids
  datadog_enabled                                = true
  datadog_advanced_enabled                       = true

  lambda_function_environment_variables = {
    ENVIRONMENT                         = var.environment
    SENTRY_DSN                          = module.sentry_initiate_indexing.sentry_key_dsn_public_output
    STEP_FUNCTION_ARN                   = data.aws_sfn_state_machine.sfn_content_index_product_metadata.arn
    AUTO_APPROVAL_STEP_FUNCTION_ARN     = data.aws_sfn_state_machine.sfn_content_auto_approve.arn
    SQS_STORE_PRODUCT_QUEUE_URL         = data.aws_sqs_queue.store_product_queue.url
  }

  msk_event_enabled                      = true
  event_source_mapping_msk_cluster_name  = var.trigger_msk_cluster_name
  event_source_mapping_msk_cluster_uuid  = var.trigger_msk_cluster_uuid
  event_source_mapping_batch_size        = 2
  kafka_topics = [var.review_queue_msk_topic]
  event_source_mapping_starting_position = "LATEST"

  iam_managed_policy_attachments = [
    aws_iam_policy.start_sfn_policy.arn,
    data.aws_iam_policy.store_product_sqs_policy.arn
  ]

  # 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_initiate_indexing" {
  source = "git@github.com:theorchard/terraform-datadog.git//modules/lambda?ref=6.13.9"

  environment                       = var.environment
  service_name                      = local.lambda_initiate_indexing
  notification_endpoints            = var.notification_endpoints
  escalation_notification_endpoints = var.escalation_notification_endpoints
}

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

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