module "hot_hits_history_rw" {
  source = "../../../modules/iam/policies/s3/buckets_rw"

  name         = "hot_hits_history_rw"
  name_prefix  = local.name_prefix
  bucket_masks = [module.hot_hits_history_bucket.bucket.id]
}

module "update_playlist_digest_rw" {
  source = "../../../modules/iam/policies/s3/buckets_rw"

  name         = "update_playlist_digest_rw"
  name_prefix  = local.name_prefix
  bucket_masks = [module.update_playlist_digest_bucket.bucket.id]
}

module "s3-track-playlist-history-rw" {
  source = "../../../modules/iam/policies/s3/buckets_rw"

  name        = "s3-track-playlist-history-rw"
  name_prefix = local.name_prefix

  bucket_masks = [
    "${local.name_prefix}-track-playlist-history"
  ]
}

module "demographic_and_skip_and_save_bkp_rw" {
  source = "../../../modules/iam/policies/s3/buckets_rw"

  name         = "demographic_and_skip_and_save_bkp"
  name_prefix  = local.name_prefix
  bucket_masks = ["prod-apollo-demographic-and-skip-and-save-bkp"]
}

module "rds_s3_export_role" {
  source        = "../../../modules/iam/roles/rds_s3_export_role"
  name          = "rds_s3_export_role"
  name_prefix   = local.name_prefix
  policies_list = [module.demographic_and_skip_and_save_bkp_rw.policy.arn]
}

module "update_playlist_digest_policy" {
  source = "../../../modules/iam/policies/athena"

  name_prefix = local.name_prefix
  policy_name = "update_playlist_digest_athena"

  athena_workspaces_arns = [
    "arn:aws:athena:${var.aws_region_id}:${local.account_id}:workgroup/primary",
  ]

  glue_resources_arns = [
    "arn:aws:glue:${var.aws_region_id}:${local.account_id}:catalog",
    "arn:aws:glue:${var.aws_region_id}:${local.account_id}:database/${aws_athena_database.update_playlist_digest_db.id}",
    "arn:aws:glue:${var.aws_region_id}:${local.account_id}:table/${aws_athena_database.update_playlist_digest_db.id}/*",
  ]
}

module "update_playlist_digest_role" {
  source = "../../../modules/iam/roles/ecs_task_role"

  enable_default_role = false
  name_prefix         = local.name_prefix
  name                = "update_playlist_digest_role"

  policies_list = [
    module.update_playlist_digest_rw.policy.arn,
    module.update_playlist_digest_policy.this.arn,
  ]

  common_tags = merge(
    local.common_tags,
    {
      project                  = "Portal",
      service                  = "ECS",
      plat_env_project_service = "${local.aggregated_tag}_PRL_ECS"
    }
  )
}

module "update_hot_hits_history_policy" {
  source = "../../../modules/iam/policies/athena"

  name_prefix = local.name_prefix
  policy_name = "update_hot_hits_athena"

  athena_workspaces_arns = [
    "arn:aws:athena:${var.aws_region_id}:${local.account_id}:workgroup/primary",
  ]

  glue_resources_arns = [
    "arn:aws:glue:${var.aws_region_id}:${local.account_id}:catalog",
    "arn:aws:glue:${var.aws_region_id}:${local.account_id}:database/${aws_athena_database.hot_hits_history_db.id}",
    "arn:aws:glue:${var.aws_region_id}:${local.account_id}:table/${aws_athena_database.hot_hits_history_db.id}/*",
  ]
}

module "update_hot_hits_history_role" {
  source = "../../../modules/iam/roles/ecs_task_role"

  enable_default_role = false
  name_prefix         = local.name_prefix
  name                = "update_hot_hits_history_role"

  policies_list = [
    module.hot_hits_history_rw.policy.arn,
    module.update_hot_hits_history_policy.this.arn,
  ]

  common_tags = merge(
    local.common_tags,
    {
      project                  = "Portal",
      service                  = "ECS",
      plat_env_project_service = "${local.aggregated_tag}_PRL_ECS"
    }
  )
}

module "update_track_playlist_history_policy" {
  source = "../../../modules/iam/policies/athena"

  name_prefix = local.name_prefix
  policy_name = "update_track_playlist_athena"

  athena_workspaces_arns = [
    "arn:aws:athena:${var.aws_region_id}:${local.account_id}:workgroup/primary",
  ]

  glue_resources_arns = [
    "arn:aws:glue:${var.aws_region_id}:${local.account_id}:catalog",
    "arn:aws:glue:${var.aws_region_id}:${local.account_id}:database/${aws_athena_database.track_playlist_history_db.id}",
    "arn:aws:glue:${var.aws_region_id}:${local.account_id}:table/${aws_athena_database.track_playlist_history_db.id}/*",
  ]
}

module "update_track_playlist_history_role" {
  source = "../../../modules/iam/roles/ecs_task_role"

  enable_default_role = false
  name_prefix         = local.name_prefix
  name                = "update_track_playlist_history_role"

  common_tags = merge(
    local.common_tags,
    {
      project                  = "Portal",
      service                  = "ECS",
      plat_env_project_service = "${local.aggregated_tag}_PRL_ECS"
    }
  )

  policies_list = [
    module.track_playlist_history_rw.policy.arn,
    module.update_track_playlist_history_policy.this.arn,
  ]
}
