module "dapd_apple_exporter_lambda_cw" {
  source              = "../../../modules/lambda/schedule_trigger_v2"
  function_name       = "${local.name_prefix}-dapd-exporter-lambda-${local.env_prefix}"
  name                = "${local.name_prefix}-dapd-exporter-dapd_apple"
  account_id          = local.account_id
  schedule_expression = "cron(*/3 * * * ? *)"
  input               = <<EOF
{
  "project": "dapd_apple"
}
EOF
}

module "dapd_spotify_exporter_lambda_cw" {
  source              = "../../../modules/lambda/schedule_trigger_v2"
  function_name       = "${local.name_prefix}-dapd-exporter-lambda-${local.env_prefix}"
  name                = "${local.name_prefix}-dapd-exporter-dapd_spotify"
  account_id          = local.account_id
  schedule_expression = "cron(*/3 * * * ? *)"
  input               = <<EOF
{
  "project": "dapd_spotify"
}
EOF
}

module "dapd_spotify_importer_lambda_cw" {
  source              = "../../../modules/lambda/schedule_trigger_v2"
  function_name       = "${local.name_prefix}-dapd-importer-lambda-${local.env_prefix}"
  name                = "${local.name_prefix}-dtf-importer-dapd_spotify"
  account_id          = local.account_id
  schedule_expression = "cron(*/3 * * * ? *)"
  input               = <<EOF
{
  "project": "dapd_spotify",
  "exporter_api_host": "${local.env_prefix}-exporter-dbx-api.delphiplatform.io"
}
EOF
}


module "dapd_apple_importer_lambda_cw" {
  source              = "../../../modules/lambda/schedule_trigger_v2"
  function_name       = "${local.name_prefix}-dapd-importer-lambda-${local.env_prefix}"
  name                = "${local.name_prefix}-dtf-importer-dapd_apple"
  account_id          = local.account_id
  schedule_expression = "cron(*/3 * * * ? *)"
  input               = <<EOF
{
  "project": "dapd_apple",
  "exporter_api_host": "${local.env_prefix}-exporter-dbx-api.delphiplatform.io"
}
EOF
}

module "dapd_importer_lambda_decibel_sf_cw" {
  source              = "../../../modules/lambda/schedule_trigger_v2"
  function_name       = "${local.name_prefix}-dapd-importer-lambda-${local.env_prefix}"
  name                = "${local.name_prefix}-dtf-importer-decibel"
  account_id          = local.account_id
  schedule_expression = "cron(5-59/3 * * * ? *)"
  input               = <<EOF
{
  "project": "decibel",
  "exporter_api_host": "${local.env_prefix}-exporter-sf-api.delphiplatform.io"
}
EOF
}

module "dapd_importer_lambda_linkfire_sf_cw" {
  source              = "../../../modules/lambda/schedule_trigger_v2"
  function_name       = "${local.name_prefix}-dapd-importer-lambda-${local.env_prefix}"
  name                = "${local.name_prefix}-dtf-importer-linkfire"
  account_id          = local.account_id
  schedule_expression = "cron(5-59/3 * * * ? *)"
  input               = <<EOF
{
  "project": "linkfire",
  "exporter_api_host": "${local.env_prefix}-exporter-sf-api.delphiplatform.io"
}
EOF
}

module "dapd_importer_lambda_cm_charts_yt_sf_cw" {
  source              = "../../../modules/lambda/schedule_trigger_v2"
  function_name       = "${local.name_prefix}-dapd-importer-lambda-${local.env_prefix}"
  name                = "${local.name_prefix}-dtf-importer-cm_charts_yt"
  account_id          = local.account_id
  schedule_expression = "cron(5-59/3 * * * ? *)"
  input               = <<EOF
{
  "project": "cm_charts_yt",
  "exporter_api_host": "${local.env_prefix}-exporter-sf-api.delphiplatform.io"
}
EOF
}

module "dapd_importer_lambda_cm_charts_shazam_sf_cw" {
  source              = "../../../modules/lambda/schedule_trigger_v2"
  function_name       = "${local.name_prefix}-dapd-importer-lambda-${local.env_prefix}"
  name                = "${local.name_prefix}-dtf-importer-cm_charts_shazam"
  account_id          = local.account_id
  schedule_expression = "cron(5-59/3 * * * ? *)"
  input               = <<EOF
{
  "project": "cm_charts_shazam",
  "exporter_api_host": "${local.env_prefix}-exporter-sf-api.delphiplatform.io"
}
EOF
}

module "dapd_importer_lambda_pd_charts_spotify_cw" {
  source              = "../../../modules/lambda/schedule_trigger_v2"
  function_name       = "${local.name_prefix}-dapd-importer-lambda-${local.env_prefix}"
  name                = "${local.name_prefix}-dtf-importer-pd_charts_spotify"
  account_id          = local.account_id
  schedule_expression = "cron(5-59/3 * * * ? *)"
  input               = <<EOF
{
  "project": "pd_charts_spotify",
  "exporter_api_host": "${local.env_prefix}-exporter-sf-api.delphiplatform.io"
}
EOF
}

module "dapd_importer_lambda_pd_charts_apple_music_cw" {
  source              = "../../../modules/lambda/schedule_trigger_v2"
  function_name       = "${local.name_prefix}-dapd-importer-lambda-${local.env_prefix}"
  name                = "${local.name_prefix}-dtf-importer-pd_charts_apple_music"
  account_id          = local.account_id
  schedule_expression = "cron(5-59/3 * * * ? *)"
  input               = <<EOF
{
  "project": "pd_charts_apple_music",
  "exporter_api_host": "${local.env_prefix}-exporter-sf-api.delphiplatform.io"
}
EOF
}

module "dapd_importer_lambda_pd_charts_tiktok_cw" {
  source              = "../../../modules/lambda/schedule_trigger_v2"
  function_name       = "${local.name_prefix}-dapd-importer-lambda-${local.env_prefix}"
  name                = "${local.name_prefix}-dtf-importer-pd_charts_tiktok"
  account_id          = local.account_id
  schedule_expression = "cron(5-59/3 * * * ? *)"
  input               = <<EOF
{
  "project": "pd_charts_tiktok",
  "exporter_api_host": "${local.env_prefix}-exporter-sf-api.delphiplatform.io"
}
EOF
}

module "dapd_importer_lambda_tiktok_top_video_cw" {
  source              = "../../../modules/lambda/schedule_trigger_v2"
  function_name       = "${local.name_prefix}-dapd-importer-lambda-${local.env_prefix}"
  name                = "${local.name_prefix}-dtf-importer-tiktok_top_video"
  account_id          = local.account_id
  schedule_expression = "cron(5-59/3 * * * ? *)"
  input               = <<EOF
{
  "project": "tiktok_top_video",
  "exporter_api_host": "${local.env_prefix}-exporter-sf-api.delphiplatform.io"
}
EOF
}

module "dapd_importer_lambda_spotify_streams_cw" {
  source              = "../../../modules/lambda/schedule_trigger_v2"
  function_name       = "${local.name_prefix}-dapd-importer-lambda-${local.env_prefix}"
  name                = "${local.name_prefix}-dtf-importer-spotify_streams"
  account_id          = local.account_id
  schedule_expression = "cron(5-59/3 * * * ? *)"
  input               = <<EOF
{
  "project": "spotify_streams",
  "exporter_api_host": "${local.env_prefix}-exporter-sf-api.delphiplatform.io"
}
EOF
}

module "dapd_importer_lambda_cm_amazon_playlists_cw" {
  source              = "../../../modules/lambda/schedule_trigger_v2"
  function_name       = "${local.name_prefix}-dapd-importer-lambda-${local.env_prefix}"
  name                = "${local.name_prefix}-dtf-importer-cm_amazon_playlists"
  account_id          = local.account_id
  schedule_expression = "cron(5-59/3 * * * ? *)"
  input               = <<EOF
{
  "project": "cm_amazon_playlists",
  "exporter_api_host": "${local.env_prefix}-exporter-sf-api.delphiplatform.io"
}
EOF
}

module "dapd_importer_lambda_cm_amazon_charts_cw" {
  source              = "../../../modules/lambda/schedule_trigger_v2"
  function_name       = "${local.name_prefix}-dapd-importer-lambda-${local.env_prefix}"
  name                = "${local.name_prefix}-dtf-importer-cm_amazon_charts"
  account_id          = local.account_id
  schedule_expression = "cron(5-59/3 * * * ? *)"
  input               = <<EOF
{
  "project": "cm_amazon_charts",
  "exporter_api_host": "${local.env_prefix}-exporter-sf-api.delphiplatform.io"
}
EOF
}

module "dapd_importer_lambda_market_size_cw" {
  source              = "../../../modules/lambda/schedule_trigger_v2"
  function_name       = "${local.name_prefix}-dapd-importer-lambda-${local.env_prefix}"
  name                = "${local.name_prefix}-dtf-importer-market_size"
  account_id          = local.account_id
  schedule_expression = "cron(5-59/3 * * * ? *)"
  input               = <<EOF
{
  "project": "market_size",
  "exporter_api_host": "${local.env_prefix}-exporter-sf-api.delphiplatform.io"
}
EOF
}

module "dapd_importer_lambda_juno_spotify_cw" {
  source              = "../../../modules/lambda/schedule_trigger_v2"
  function_name       = "${local.name_prefix}-dapd-importer-lambda-${local.env_prefix}"
  name                = "${local.name_prefix}-dtf-importer-juno_spotify"
  account_id          = local.account_id
  schedule_expression = "cron(5-59/3 * * * ? *)"
  input               = <<EOF
{
  "project": "juno_spotify",
  "exporter_api_host": "${local.env_prefix}-exporter-sf-api.delphiplatform.io"
}
EOF
}


data "aws_s3_bucket" "dapd_etl_export" {
  bucket = "${local.name_prefix}-public-data-etl-export"
}

data "aws_sqs_queue" "s3_decibel" {
  name = "${local.name_prefix}-public-data-exporter-s3-events"
}

resource "aws_s3_bucket_notification" "dapd_etl_export" {
  bucket = data.aws_s3_bucket.dapd_etl_export.id

  queue {
    queue_arn     = var.snowflake_sqs_arn
    events        = ["s3:ObjectCreated:*"]
    filter_prefix = "project%3Ddapd/"
  }

  queue {
    queue_arn     = data.aws_sqs_queue.s3_decibel.arn
    events        = ["s3:ObjectCreated:*"]
    filter_prefix = "CHARTS_SPOTIFY/"
  }

  queue {
    queue_arn     = data.aws_sqs_queue.s3_decibel.arn
    events        = ["s3:ObjectCreated:*"]
    filter_prefix = "CHARTS_APPLE/"
  }

  queue {
    queue_arn     = data.aws_sqs_queue.s3_decibel.arn
    events        = ["s3:ObjectCreated:*"]
    filter_prefix = "CHARTS_TIKTOK/"
  }

  queue {
    queue_arn     = data.aws_sqs_queue.s3_decibel.arn
    events        = ["s3:ObjectCreated:*"]
    filter_prefix = "JUNO/"
  }

  queue {
    queue_arn     = data.aws_sqs_queue.s3_decibel.arn
    events        = ["s3:ObjectCreated:*"]
    filter_prefix = "MARKET_SIZE/"
  }
}

data "aws_s3_bucket" "dapd_juno_export" {
  bucket = "${local.name_prefix}-spotify-juno-export"
}

resource "aws_s3_bucket_notification" "dapd_etl_juno_export" {
  bucket = data.aws_s3_bucket.dapd_juno_export.id

  queue {
    queue_arn = data.aws_sqs_queue.s3_decibel.arn
    events    = ["s3:ObjectCreated:*"]
  }
}

data "aws_s3_bucket" "dapd_market_size_export" {
  bucket = "${local.name_prefix}-market-size-export"
}

resource "aws_s3_bucket_notification" "dapd_market_size_export" {
  bucket = data.aws_s3_bucket.dapd_market_size_export.id

  queue {
    queue_arn = data.aws_sqs_queue.s3_decibel.arn
    events    = ["s3:ObjectCreated:*"]
  }
}

moved {
  from = aws_lambda_event_source_mapping.dapd_exporter_lambda_event_mapping
  to   = aws_lambda_event_source_mapping.dapd_exporter_dtf_sqs
}

resource "aws_lambda_event_source_mapping" "dapd_exporter_dtf_sqs" {
  event_source_arn = data.aws_sqs_queue.s3_decibel.arn
  function_name    = "${local.name_prefix}-dapd-exporter-lambda-${local.env_prefix}"
  batch_size       = 1
  enabled          = true
}

module "notification_lambda_cw" {
  source              = "../../../modules/lambda/schedule_trigger_v2"
  function_name       = "${local.name_prefix}-notification-service-${local.env_prefix}"
  account_id          = local.account_id
  schedule_expression = "cron(*/5 * * * ? *)"
}
