# dtf-notification
resource "aws_sqs_queue" "dtf_notification" {
  name                        = "${local.name_prefix}-dtf-notification.fifo"
  visibility_timeout_seconds  = 900
  message_retention_seconds   = 345600
  receive_wait_time_seconds   = 20
  fifo_queue                  = true
  content_based_deduplication = true
  kms_master_key_id           = data.aws_kms_key.custom_default_sqs_key.id

  /*
  redrive_policy = jsonencode({
    deadLetterTargetArn = aws_sqs_queue.dtf_notification_dlq.arn
    maxReceiveCount     = 5
  }) */

  tags = merge(
    local.common_tags,
    {
      project                  = "DAPD",
      service                  = "SQS",
      plat_env_project_service = "${local.aggregated_tag}_DAPD_SQS"
    }
  )
}

# sf-notification
resource "aws_sqs_queue" "sf_notification" {
  name                       = "${local.name_prefix}-sf-notification"
  visibility_timeout_seconds = 900
  message_retention_seconds  = 345600
  receive_wait_time_seconds  = 20
  kms_master_key_id          = data.aws_kms_key.custom_default_sqs_key.id

  redrive_policy = jsonencode({
    deadLetterTargetArn = aws_sqs_queue.sf_notification_dlq.arn
    maxReceiveCount     = 5
  })

  tags = merge(
    local.common_tags,
    {
      project                  = "DAPD",
      service                  = "SQS",
      plat_env_project_service = "${local.aggregated_tag}_DAPD_SQS"
    }
  )
}

data "aws_iam_policy_document" "sf_notification" {
  version = "2012-10-17"

  statement {
    actions = [
      "sqs:SendMessage"
    ]
    condition {
      test     = "ArnLike"
      variable = "aws:SourceArn"
      values = [
        "arn:aws:s3:::${local.name_prefix}-playlist-notifications",
      ]
    }
    effect = "Allow"
    principals {
      type        = "AWS"
      identifiers = ["*"]
    }
    resources = [
      aws_sqs_queue.sf_notification.arn,
    ]
    sid = "${local.name_prefix}-sf-notification-s3-events"
  }
}

resource "aws_sqs_queue_policy" "sf_notification" {
  queue_url = aws_sqs_queue.sf_notification.id
  policy    = data.aws_iam_policy_document.sf_notification.json
}

resource "aws_sqs_queue" "sf_notification_dlq" {
  name              = "${local.name_prefix}-sf-notification-dlq"
  kms_master_key_id = data.aws_kms_key.custom_default_sqs_key.id

  tags = merge(
    local.common_tags,
    {
      project                  = "DAPD",
      service                  = "SQS",
      plat_env_project_service = "${local.aggregated_tag}_DAPD_SQS"
    }
  )
}

data "aws_iam_policy_document" "sqs_notification_test_policy" {
  version = "2012-10-17"

  statement {
    actions = [
      "sqs:SendMessage"
    ]
    condition {
      test     = "ArnLike"
      variable = "aws:SourceArn"
      values = [
        "arn:aws:sns:${var.aws_region_id}:${local.account_id}:${local.name_prefix}-*"
      ]
    }
    effect = "Allow"
    principals {
      type        = "Service"
      identifiers = ["sns.amazonaws.com"]
    }
    resources = [
      aws_sqs_queue.notification_test.arn,
    ]
    sid = "${local.name_prefix}-sns-send"
  }
}

resource "aws_sqs_queue_policy" "notification_test" {
  queue_url = aws_sqs_queue.notification_test.id
  policy    = data.aws_iam_policy_document.sqs_notification_test_policy.json
}

resource "aws_sqs_queue" "notification_test" {
  name              = "${local.name_prefix}-notification-test"
  kms_master_key_id = data.aws_kms_key.custom_default_sqs_key.id

  tags = merge(
    local.common_tags,
    {
      project                  = "DAPD",
      service                  = "SQS",
      plat_env_project_service = "${local.aggregated_tag}_DAPD_SQS"
    }
  )
}

resource "aws_sns_topic_subscription" "notification_test" {
  topic_arn = aws_sns_topic.notification.arn
  protocol  = "sqs"
  endpoint  = aws_sqs_queue.notification_test.arn
}
