module "s3_dms_replication" {
  source             = "../../../modules/s3/private_bucket_with_logs_aes256_v2"
  bucket_name        = "${local.name_prefix}-dms-replication"
  logs_bucket_id     = data.aws_s3_bucket.security_logs.id
  logs_object_prefix = "${local.name_prefix}/dms-replication/"
  versioning         = true

  common_tags = {
    project                  = "Infrastructure",
    service                  = "S3",
    plat_env_project_service = "${local.aggregated_tag}_INFRA_S3"
  }
}

resource "aws_s3_bucket_replication_configuration" "replication" {
  depends_on = [module.s3_dms_replication]

  role   = module.iam_s3_dms_replication_role.role.arn
  bucket = module.s3_dms_replication.bucket.id

  dynamic "rule" {
    for_each = local.delphi_replica_buckets
    content {
      id       = rule.value
      status   = "Disabled"
      priority = index(local.delphi_replica_buckets, rule.value)

      delete_marker_replication {
        status = "Enabled"
      }

      filter {}

      destination {
        bucket        = "arn:aws:s3:::${rule.value}"
        account       = local.delphi_replica_bucket_accounts[rule.value]
        storage_class = "STANDARD"

        access_control_translation {
          owner = "Destination"
        }
      }
    }
  }
}
