resource "aws_lambda_event_source_mapping" "this" {
  event_source_arn = var.queue_arn
  function_name    = var.lambda_arn
  batch_size       = var.batch_size
  enabled          = true

  dynamic "scaling_config" {
    for_each = var.maximum_concurrency != null ? [1] : []
    content {
      maximum_concurrency = var.maximum_concurrency
    }
  }
}

resource "aws_iam_role_policy" "allow_sqs" {
  role = var.lambda_role_name

  policy = jsonencode({
    Version = "2012-10-17"
    Statement = [{
      Effect = "Allow"
      Action = [
        "sqs:ReceiveMessage",
        "sqs:DeleteMessage",
        "sqs:GetQueueAttributes"
      ]
      Resource = var.queue_arn
    }]
  })
}
