
resource "aws_sqs_queue" "songwhip_presaves_dlq" {
  for_each = toset(local.presave_stores)

  name                       = "${var.environment}-${var.service_name}-${each.value}-deadletter"
  visibility_timeout_seconds = 300
  # If dql messages aren't processed in 4 days,
  # something is probably wrong and we want them to be automatically deleted to avoid backlogs.
  message_retention_seconds = 345600
  max_message_size          = var.sqs_max_message_size
  sqs_managed_sse_enabled   = true
  tags                      = module.default_tags.tags
}

resource "aws_sqs_queue" "songwhip_presaves_sqs" {
  for_each = local.presave_store_client_pairs

  name                       = "${var.environment}-${var.service_name}-${each.value.store}-${each.value.client}"
  visibility_timeout_seconds = 360
  # If messages aren't processed in 4 days,
  # something is probably wrong and we want them to be automatically deleted to avoid backlogs.
  message_retention_seconds = 345600
  max_message_size          = var.sqs_max_message_size
  sqs_managed_sse_enabled   = true
  tags                      = module.default_tags.tags
  redrive_policy = jsonencode({
    deadLetterTargetArn = aws_sqs_queue.songwhip_presaves_dlq[each.value.store].arn
    maxReceiveCount     = 3
  })
}

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

  environment        = var.environment
  application_family = var.application_family
  queue_name         = "${var.service_name}-execution-results"

  sqs_visibility_timeout_seconds = 360
  sqs_max_message_size           = var.sqs_max_message_size
  sqs_deadletter_enabled         = true

  # This dlq is the "last stop" for failed executions. 
  # If messages end up here, it indicates a problem with the presaves service that needs to be investigated,
  # so we set a longer retention period to be able to analyze the failed messages.
  deadletter_message_retention_seconds = 604800 // 7 days
  deadletter_max_receive_count         = 3
  deadletter_max_message_size          = var.sqs_max_message_size
}

data "aws_iam_policy_document" "songwhip_presaves_sqs_policy_document" {
  for_each = aws_sqs_queue.songwhip_presaves_sqs

  statement {
    actions = [
      "sqs:SendMessage",
      "sqs:SendMessageBatch",
      "sqs:PurgeQueue",
      "sqs:GetQueueUrl",
      "sqs:GetQueueAttributes",
    ]
    resources = [each.value.arn]
  }
}

data "aws_iam_policy_document" "songwhip_presaves_sqs_dlq_policy_document" {
  for_each = aws_sqs_queue.songwhip_presaves_dlq

  statement {
    actions = [
      "sqs:GetQueueUrl",
      "sqs:GetQueueAttributes",
    ]
    resources = [each.value.arn]
  }
}

data "aws_iam_policy_document" "songwhip_presaves_execution_results_sqs_send_policy_document" {
  statement {
    actions = [
      "sqs:SendMessage",
      "sqs:SendMessageBatch",
      "sqs:PurgeQueue",
      "sqs:GetQueueUrl",
      "sqs:GetQueueAttributes",
    ]
    resources = [module.songwhip_presaves_execution_results_sqs.queue_arn]
  }
}

data "aws_iam_policy_document" "songwhip_presaves_execution_results_sqs_attributes_policy_document" {
  statement {
    actions = [
      "sqs:GetQueueUrl",
      "sqs:GetQueueAttributes",
    ]
    resources = [
      module.songwhip_presaves_execution_results_sqs.queue_arn,
      module.songwhip_presaves_execution_results_sqs.deadletter_queue_arn
    ]
  }
}

resource "aws_iam_policy" "songwhip_presaves_sqs_send_policy" {
  for_each    = data.aws_iam_policy_document.songwhip_presaves_sqs_policy_document
  name        = "${var.environment}-${var.service_name}-${each.key}-sqs-send-messages"
  description = "Allows sending messages to the ${each.key} SQS queue"
  policy      = each.value.json
}

resource "aws_iam_policy" "songwhip_presaves_sqs_dlq_attributes_policy" {
  for_each    = data.aws_iam_policy_document.songwhip_presaves_sqs_dlq_policy_document
  name        = "${var.environment}-${var.service_name}-${each.key}-sqs-read-dlq"
  description = "Allows reading attributes for the ${each.key} SQS deadletter queue"
  policy      = each.value.json
}

resource "aws_iam_policy" "songwhip_presaves_execution_results_sqs_send_policy" {
  name        = "${var.environment}-${var.service_name}-execution-results-sqs-send-messages"
  description = "Allows sending messages to execution results SQS queue"
  policy      = data.aws_iam_policy_document.songwhip_presaves_execution_results_sqs_send_policy_document.json
}

resource "aws_iam_policy" "songwhip_presaves_execution_results_sqs_attributes_policy" {
  name        = "${var.environment}-${var.service_name}-execution-results-sqs-read"
  description = "Allows reading attributes for the execution results SQS queue and its deadletter queue"
  policy      = data.aws_iam_policy_document.songwhip_presaves_execution_results_sqs_attributes_policy_document.json
}
