resource "aws_cloudwatch_event_rule" "spotify_event_rule" {
  name                = "${local.name_prefix}-agg_cp_data_sf_to_mysql_spotify"
  schedule_expression = "cron(0 9 * * ? *)"
  is_enabled          = true
}

resource "aws_cloudwatch_event_target" "spotify_event_target" {
  target_id = "SpotifyTarget"
  arn       = aws_sfn_state_machine.step_function.id
  role_arn  = aws_iam_role.execute_state_machine_role.arn
  rule      = aws_cloudwatch_event_rule.spotify_event_rule.name
  input     = jsonencode({ "dsp" : "spotify", "data_type" : "tracks_top_playlists_aggregated_history", "config" : { "snowflake_wait_seconds" : 600, "mysql_wait_seconds" : 900 } })
}

resource "aws_cloudwatch_event_rule" "weekly_top_spotify_event_rule" {
  name                = "${local.name_prefix}-agg_cp_data_sf_to_mysql_wkl_top_spotify"
  schedule_expression = "cron(20 9 * * ? *)"
  is_enabled          = true
}

resource "aws_cloudwatch_event_target" "weekly_top_spotify_event_target" {
  target_id = "SpotifyTarget"
  arn       = aws_sfn_state_machine.step_function.id
  role_arn  = aws_iam_role.execute_state_machine_role.arn
  rule      = aws_cloudwatch_event_rule.weekly_top_spotify_event_rule.name
  input     = jsonencode({ "dsp" : "spotify", "data_type" : "weekly_top_playlists", "config" : { "snowflake_wait_seconds" : 120, "mysql_wait_seconds" : 180 } })
}

resource "aws_cloudwatch_event_rule" "apple_event_rule" {
  name                = "${local.name_prefix}-agg_cp_data_sf_to_mysql_apple"
  schedule_expression = "cron(30 9 * * ? *)"
  is_enabled          = true
}

resource "aws_cloudwatch_event_target" "apple_event_target" {
  target_id = "AppleTarget"
  arn       = aws_sfn_state_machine.step_function.id
  role_arn  = aws_iam_role.execute_state_machine_role.arn
  rule      = aws_cloudwatch_event_rule.apple_event_rule.name
  input     = jsonencode({ "dsp" : "apple", "data_type" : "tracks_top_playlists_aggregated_history", "config" : { "snowflake_wait_seconds" : 600, "mysql_wait_seconds" : 1000 } })
}

resource "aws_cloudwatch_event_rule" "weekly_top_apple_event_rule" {
  name                = "${local.name_prefix}-agg_cp_data_sf_to_mysql_wkl_top_apple"
  schedule_expression = "cron(50 9 * * ? *)"
  is_enabled          = true
}

resource "aws_cloudwatch_event_target" "weekly_top_apple_event_target" {
  target_id = "AppleTarget"
  arn       = aws_sfn_state_machine.step_function.id
  role_arn  = aws_iam_role.execute_state_machine_role.arn
  rule      = aws_cloudwatch_event_rule.weekly_top_apple_event_rule.name
  input     = jsonencode({ "dsp" : "apple", "data_type" : "weekly_top_playlists", "config" : { "snowflake_wait_seconds" : 120, "mysql_wait_seconds" : 180 } })
}

resource "aws_cloudwatch_event_rule" "streams_event_rule" {
  name                = "${local.name_prefix}-agg_cp_data_sf_to_mysql_streams"
  schedule_expression = "cron(0 10 * * ? *)"
  is_enabled          = false // must be disabled before the release
}

resource "aws_cloudwatch_event_target" "streams_event_target" {
  target_id = "AppleTarget"
  arn       = aws_sfn_state_machine.step_function.id
  role_arn  = aws_iam_role.execute_state_machine_role.arn
  rule      = aws_cloudwatch_event_rule.streams_event_rule.name
  input     = jsonencode({ "config" : { "mysql_wait_seconds" : 600, "snowflake_wait_seconds" : 600 }, "data_type" : "playlist_digest_metrics", "metric" : "streams" })
}

resource "aws_cloudwatch_event_rule" "followers_event_rule" {
  name                = "${local.name_prefix}-agg_cp_data_sf_to_mysql_followers"
  schedule_expression = "cron(20 10 * * ? *)"
  is_enabled          = false // must be disabled before the release
}

resource "aws_cloudwatch_event_target" "followers_event_target" {
  target_id = "AppleTarget"
  arn       = aws_sfn_state_machine.step_function.id
  role_arn  = aws_iam_role.execute_state_machine_role.arn
  rule      = aws_cloudwatch_event_rule.followers_event_rule.name
  input     = jsonencode({ "config" : { "mysql_wait_seconds" : 180, "snowflake_wait_seconds" : 120 }, "data_type" : "playlist_digest_metrics", "metric" : "followers" })
}

resource "aws_cloudwatch_event_rule" "playlist_digest_playlists_event_rule" {
  name                = "${local.name_prefix}-agg_cp_data_sf_to_mysql_playlist_digest_playlists"
  schedule_expression = "cron(30 10 * * ? *)"
  is_enabled          = false // must be disabled before the release
}

resource "aws_cloudwatch_event_target" "playlist_digest_playlists_event_target" {
  target_id = "AppleTarget"
  arn       = aws_sfn_state_machine.step_function.id
  role_arn  = aws_iam_role.execute_state_machine_role.arn
  rule      = aws_cloudwatch_event_rule.playlist_digest_playlists_event_rule.name
  input     = jsonencode({ "config" : { "mysql_wait_seconds" : 180, "snowflake_wait_seconds" : 120 }, "data_type" : "playlist_digest_playlists" })
}
