module "lambda-nr-ownership-ingest-ar-rights-filter" {
  source                      = "git@github.com:theorchard/terraform-lambda.git//?ref=4.0.1"
  environment                 = var.environment
  lambda_name                 = var.ar_rights_filter_lambda_name
  lambda_description          = "Consumes cdc art relation rights data."
  lambda_function_timeout     = "900"
  use_container_image         = true
  vpc_subnet_ids              = module.vpc_info.default_private_subnet_ids
  vpc_enabled                 = true
  vpc_id                      = module.vpc_info.vpc_id
  datadog_advanced_enabled    = true
  application_family          = var.application_family
  lambda_function_memory_size = var.lambda_memory_size
  splitio_enabled             = true

  msk_event_enabled                      = var.msk_event_enabled
  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        = 10
  kafka_topics                           = [var.ar_track_master_rights_topic, var.ar_vendor_contract_topic, var.ar_release_territory_restriction_topic, var.ar_subaccount_royalty_collection_topic, var.ar_subaccount_royalty_collection_territories_topic, var.project_used_to_belong_to_msk_topic_v5]
  event_source_mapping_starting_position = "LATEST"

  # MSK ESM filters can only match on the message value, not the topic/key, so a
  # single $or pattern is applied across all topics on this mapping. The 5 AR CDC
  # topics carry no `event.type` field, so they always match the first clause and
  # pass through unfiltered. Only the projectUsedToBelongTo v5 topic is actually
  # filtered: USED_TO_BELONG_TO events are dropped unless the operation is CREATE.
  #   - no event.type            -> AR CDC topics, allow everything
  #   - event.type != USED_TO... -> any other Neo4j CDC event type, allow
  #   - USED_TO_BELONG_TO        -> allow only CREATE (drop UPDATE/DELETE)
  event_source_mapping_filter_criteria_pattern = jsonencode({
    value = {
      "$or" = [
        { "event.type" = [{ "exists" = false }] },
        { "event.type" = [{ "anything-but" = ["USED_TO_BELONG_TO"] }] },
        { "event.type" = ["USED_TO_BELONG_TO"], "event.operation" = ["CREATE"] },
      ]
    }
  })

  lambda_function_environment_variables = {
    Environment        = var.environment
    DD_LAMBDA_HANDLER  = "app.handler"
    SENTRY_DSN         = module.lambda_nr_ownership_ingest_ar_rights_filter_sentry_project.sentry_key_dsn_public_output
    SPLIT_API_KEY_PATH = "${var.environment}/split/API_KEY"
    BUCKET_NAME        = data.aws_s3_bucket.s3_bucket.bucket
    KAFKAJS_LOG_LEVEL  = "nothing"
    KAFKA_BROKERS      = var.kafka_brokers
    SQS_QUEUE_URL      = module.sqs_sync_rights_queue.queue_url
    SQS_RELEASES_URL   = module.sqs_filter_products_queue.queue_url
    SQS_PRODUCT_TRANSFER_URL = module.sqs_product_transfer_queue.queue_url
    LOGGER_LEVEL       = "info"
    SEND_TO_SQS        = true
  }

  lambda_function_reserved_concurrent_executions = 1

  iam_managed_policy_attachments = [
    data.aws_iam_policy.ownership_ingestion_bucket_rw_policy.arn,
    module.sqs_sync_rights_queue.sqs_minimal_policy_arn_output,
    module.sqs_filter_products_queue.sqs_minimal_policy_arn_output,
    module.sqs_product_transfer_queue.sqs_minimal_policy_arn_output
  ]
}

module "lambda_nr_ownership_ingest_ar_rights_filter_sentry_project" {
  source                       = "git@github.com:theorchard/terraform-sentry.git//?ref=4.1.2"
  service_name                 = var.ar_rights_filter_lambda_name
  secrets_manager_service_name = var.ar_rights_filter_lambda_name
  environment                  = var.environment
  platform                     = "node"
  application_family           = var.application_family
}

module "datadog_lambda_nr_ownership_ingest_ar_rights_filter_dashboards" {
  source                 = "git@github.com:theorchard/terraform-datadog.git//modules/lambda?ref=6.13.4"
  service_name           = var.ar_rights_filter_lambda_name
  environment            = var.environment
  notification_endpoints = var.slack_alert_channel
  # Some errors are expected on this service due to QA vendor contract data
  lambda_error_warning_number          = 180
  lambda_error_critical_number         = 200
  teams                                = [var.team_name]
}
