resource "aws_cloudwatch_event_bus" "nmf_event_bus" {
  name = "${local.name_prefix}-new_music_friday"
}

resource "aws_cloudwatch_event_rule" "nmf_event_bus_debug_rule" {
  name        = "${local.name_prefix}-new_music_friday"
  description = "Capture all event and send them to CloudWatch"

  event_bus_name = aws_cloudwatch_event_bus.nmf_event_bus.name
  event_pattern  = <<EOF
{
  "account": [
    "801577982711"
  ]
}
EOF

  tags = merge(
    local.common_tags,
    {
      project                  = "Infrastructure",
      service                  = "EventBridge",
      plat_env_project_service = "${local.aggregated_tag}_INFRA_EB"
    }
  )
}

resource "aws_cloudwatch_event_target" "nmf_event_bus_cloudwatch_target" {
  rule           = aws_cloudwatch_event_rule.nmf_event_bus_debug_rule.name
  event_bus_name = aws_cloudwatch_event_bus.nmf_event_bus.name
  arn            = aws_cloudwatch_log_group.nmf_event_bus.arn
  target_id      = "${local.name_prefix}-new_music_friday"
}

data "aws_iam_policy_document" "nmf_event_bus_publisher" {
  version = "2012-10-17"
  statement {
    actions = [
      "events:DescribeEventBus",
      "events:ListEventBuses",
      "events:PutEvents",
    ]
    effect = "Allow"
    resources = [
      aws_cloudwatch_event_bus.nmf_event_bus.arn
    ]
  }
}

resource "aws_iam_policy" "nmf_event_bus_publisher" {
  name        = "${local.name_prefix}-nmf_event_bus_publisher"
  description = "Allow publish to NMF event bus."
  policy      = data.aws_iam_policy_document.nmf_event_bus_publisher.json
}

// eventbus for notifications
resource "aws_cloudwatch_event_bus" "notifications_event_bus" {
  name = "${local.name_prefix}-notifications"
}

resource "aws_cloudwatch_event_rule" "notifications_debug_rule" {
  name        = "${local.name_prefix}-notifications"
  description = "Capture all event and send them to CloudWatch"

  event_bus_name = aws_cloudwatch_event_bus.notifications_event_bus.name
  event_pattern  = <<EOF
{
  "account": [
    "725764004555"
  ]
}
EOF

  tags = merge(
    local.common_tags,
    {
      project                  = "Infrastructure",
      service                  = "EventBridge",
      plat_env_project_service = "${local.aggregated_tag}_INFRA_EB"
    }
  )
}

resource "aws_cloudwatch_event_target" "notifications_cloudwatch_target" {
  rule           = aws_cloudwatch_event_rule.notifications_debug_rule.name
  event_bus_name = aws_cloudwatch_event_bus.notifications_event_bus.name
  arn            = aws_cloudwatch_log_group.notifications_event_bus.arn
  target_id      = "${local.name_prefix}-cw_notifications"
}

data "aws_iam_policy_document" "notifications_event_bus_publisher" {
  version = "2012-10-17"
  statement {
    actions = [
      "events:DescribeEventBus",
      "events:ListEventBuses",
      "events:PutEvents",
    ]
    effect = "Allow"
    resources = [
      aws_cloudwatch_event_bus.notifications_event_bus.arn
    ]
  }
}

resource "aws_iam_policy" "notifications_event_bus_publisher" {
  name        = "${local.name_prefix}-notifications_event_bus_publisher"
  description = "Allow publish to notifications event bus."
  policy      = data.aws_iam_policy_document.notifications_event_bus_publisher.json
}

resource "aws_cloudwatch_event_rule" "charts_messages_rule" {
  name        = "${local.name_prefix}-charts_messages"
  description = "Capture charts messages events and send them to SQS"

  event_bus_name = aws_cloudwatch_event_bus.notifications_event_bus.name
  event_pattern  = <<EOF
{
  "account": [
    "725764004555"
  ],
  "event": {
    "app": ["apollo"],
    "code": ["charts_updated"]
  }
}
EOF

  tags = merge(
    local.common_tags,
    {
      project                  = "Infrastructure",
      service                  = "EventBridge",
      plat_env_project_service = "${local.aggregated_tag}_INFRA_EB"
    }
  )
}

resource "aws_cloudwatch_event_target" "charts_entry_messages_event_bus_sqs_target" {
  rule           = aws_cloudwatch_event_rule.charts_messages_rule.name
  event_bus_name = aws_cloudwatch_event_bus.notifications_event_bus.name
  arn            = module.charts_entry_messages_event_bus_queue.queue.arn
  target_id      = "${local.name_prefix}-charts_entry_messages_sqs"
  dead_letter_config {
    arn = module.charts_entry_messages_event_bus_dlq.queue.arn
  }
}

resource "aws_cloudwatch_event_target" "charts_exit_messages_event_bus_sqs_target" {
  rule           = aws_cloudwatch_event_rule.charts_messages_rule.name
  event_bus_name = aws_cloudwatch_event_bus.notifications_event_bus.name
  arn            = module.charts_exit_messages_event_bus_queue.queue.arn
  target_id      = "${local.name_prefix}-charts_exit_messages_sqs"
  dead_letter_config {
    arn = module.charts_exit_messages_event_bus_dlq.queue.arn
  }
}

resource "aws_cloudwatch_event_target" "charts_move_messages_event_bus_sqs_target" {
  rule           = aws_cloudwatch_event_rule.charts_messages_rule.name
  event_bus_name = aws_cloudwatch_event_bus.notifications_event_bus.name
  arn            = module.charts_move_messages_event_bus_queue.queue.arn
  target_id      = "${local.name_prefix}-charts_move_messages_sqs"
  dead_letter_config {
    arn = module.charts_move_messages_event_bus_dlq.queue.arn
  }
}

resource "aws_cloudwatch_event_rule" "charts_feed_messages_rule" {
  name        = "${local.name_prefix}-charts_feed_messages"
  description = "Capture charts feed messages events and send them to SQS"

  event_bus_name = aws_cloudwatch_event_bus.notifications_event_bus.name
  event_pattern  = <<EOF
{
  "account": [
    "725764004555"
  ],
  "event": {
    "app": ["apollo"],
    "code": ["messages_created"]
  }
}
EOF

  tags = merge(
    local.common_tags,
    {
      project                  = "Infrastructure",
      service                  = "EventBridge",
      plat_env_project_service = "${local.aggregated_tag}_INFRA_EB"
    }
  )
}

resource "aws_cloudwatch_event_target" "charts_feed_messages_event_bus_sqs_target" {
  rule           = aws_cloudwatch_event_rule.charts_feed_messages_rule.name
  event_bus_name = aws_cloudwatch_event_bus.notifications_event_bus.name
  arn            = module.charts_feed_messages_event_bus_queue.queue.arn
  target_id      = "${local.name_prefix}-charts_feed_messages_sqs"
  dead_letter_config {
    arn = module.charts_feed_messages_event_bus_dlq.queue.arn
  }
}

resource "aws_cloudwatch_event_rule" "charts_push_messages_rule" {
  name        = "${local.name_prefix}-charts_push_messages"
  description = "Capture charts push messages events and send them to SQS"

  event_bus_name = aws_cloudwatch_event_bus.notifications_event_bus.name
  event_pattern  = <<EOF
{
  "account": [
    "725764004555"
  ],
  "event": {
    "app": ["apollo"],
    "code": ["feed_messages_created"]
  }
}
EOF

  tags = merge(
    local.common_tags,
    {
      project                  = "Infrastructure",
      service                  = "EventBridge",
      plat_env_project_service = "${local.aggregated_tag}_INFRA_EB"
    }
  )
}

resource "aws_cloudwatch_event_target" "charts_push_messages_event_bus_sqs_target" {
  rule           = aws_cloudwatch_event_rule.charts_push_messages_rule.name
  event_bus_name = aws_cloudwatch_event_bus.notifications_event_bus.name
  arn            = module.charts_push_messages_event_bus_queue.queue.arn
  target_id      = "${local.name_prefix}-charts_push_messages_sqs"
  dead_letter_config {
    arn = module.charts_push_messages_event_bus_dlq.queue.arn
  }
}

resource "aws_cloudwatch_event_rule" "juno_updated_messages_rule" {
  name        = "${local.name_prefix}-juno_updated"
  description = "Capture juno updated events and send them to SQS"

  event_bus_name = aws_cloudwatch_event_bus.notifications_event_bus.name
  event_pattern  = <<EOF
{
  "account": [
    "725764004555"
  ],
  "event": {
    "app": ["apollo"],
    "code": ["juno_updated"]
  }
}
EOF

  tags = merge(
    local.common_tags,
    {
      project                  = "Infrastructure",
      service                  = "EventBridge",
      plat_env_project_service = "${local.aggregated_tag}_INFRA_EB"
    }
  )
}

resource "aws_cloudwatch_event_target" "juno_updated_event_bus_sqs_target" {
  rule           = aws_cloudwatch_event_rule.juno_updated_messages_rule.name
  event_bus_name = aws_cloudwatch_event_bus.notifications_event_bus.name
  arn            = module.juno_updated_event_bus_queue.queue.arn
  target_id      = "${local.name_prefix}-juno_updated_sqs"
  dead_letter_config {
    arn = module.juno_updated_event_bus_dlq.queue.arn
  }
}

resource "aws_cloudwatch_event_rule" "juno_filter_notifications_rule" {
  name        = "${local.name_prefix}-juno_filter_notification"
  description = "Capture juno filter notifications events and send them to SQS"

  event_bus_name = aws_cloudwatch_event_bus.notifications_event_bus.name
  event_pattern  = <<EOF
{
  "account": [
    "725764004555"
  ],
  "event": {
    "app": ["apollo"],
    "code": ["messages_created"],
    "subject": ["juno_digest"]
  }
}
EOF

  tags = merge(
    local.common_tags,
    {
      project                  = "Infrastructure",
      service                  = "EventBridge",
      plat_env_project_service = "${local.aggregated_tag}_INFRA_EB"
    }
  )
}

resource "aws_cloudwatch_event_target" "juno_filter_notifications_event_bus_sqs_target" {
  rule           = aws_cloudwatch_event_rule.juno_filter_notifications_rule.name
  event_bus_name = aws_cloudwatch_event_bus.notifications_event_bus.name
  arn            = module.juno_filter_notifications_event_bus_queue.queue.arn
  target_id      = "${local.name_prefix}-juno_filter_notifications_sqs"
  dead_letter_config {
    arn = module.juno_filter_notifications_event_bus_dlq.queue.arn
  }
}
