locals {
  get_indexed_products_lambda_name = "${var.service_name}-get-indexed-products"
}

data "aws_sqs_queue" "content_review_index_update_queue" {
  name = "${var.environment}-content-review-index-update-queue"
}

data "aws_iam_policy_document" "content_review_index_update_queues_policy_document" {
  statement {
    effect = "Allow"
    actions = [
      "sqs:GetQueueAttributes",
      "sqs:GetQueueUrl",
      "sqs:ListQueueTags",
      "sqs:TagQueue",
      "sqs:UntagQueue",
      "sqs:SendMessageBatch",
      "sqs:SendMessage"
    ]
    resources = [
      data.aws_sqs_queue.content_review_index_update_queue.arn
    ]
  }
}

resource "aws_iam_policy" "content_review_index_update_queue_policy" {
  name        = "SQS-${var.environment}-${local.get_indexed_products_lambda_name}"
  description = "Get indexed products lambda policy"
  policy      = data.aws_iam_policy_document.content_review_index_update_queues_policy_document.json
}

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

  environment        = var.environment
  application_family = var.application_family
  lambda_name        = local.get_indexed_products_lambda_name

  use_container_image                            = true
  lambda_description                             = "Gets all products indexed in opensearch."
  lambda_function_timeout                        = "900"
  lambda_function_reserved_concurrent_executions = 10
  vpc_enabled                                    = true
  vpc_id                                         = module.vpc_info.vpc_id
  vpc_subnet_ids                                 = module.vpc_info.default_private_subnet_ids
  datadog_enabled                                = true
  datadog_advanced_enabled                       = true
  additional_tags                                = var.additional_tags

  splitio_enabled = true

  cloudwatch_event_enabled  = true
  cloudwatch_event_schedule = "cron(15 16 * * ? *)"

  iam_managed_policy_attachments = [
    data.aws_iam_policy.opensearch_rw_policy.arn,
    aws_iam_policy.content_review_index_update_queue_policy.arn
  ]

  lambda_function_environment_variables = {
    ENVIRONMENT            = var.environment
    SENTRY_DSN             = module.sentry_lambda_content_get_indexed_products.sentry_key_dsn_public_output
    SQS_QUEUE_URL          = data.aws_sqs_queue.content_review_index_update_queue.url
    OPENSEARCH_ENDPOINT    = var.os_domain_endpoint
    OPENSEARCH_INDEX_ALIAS = var.opensearch_index_read_alias
    OPENSEARCH_PORT        = var.os_domain_port
  }

}

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

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

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

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