terraform {
  backend "s3" {
    bucket                  = "dev-dna-tfstate"
    key                     = "Apollo/Development/dynamodb.tfstate"
    region                  = "us-east-1"
    shared_credentials_file = "~/.aws/credentials"
    profile                 = "gdb-delphi-dev"
    dynamodb_table          = "terraform-state-lock-dynamodb"
  }
}

provider "aws" {
  profile = var.aws_profile
  region  = var.aws_region_id

}

locals {
  name_prefix = lower("${var.platform["prefix"]}-${var.platform["name"]}")
  common_tags = {
    platform                 = var.platform.name
    environment              = var.platform.environment
    platform_with_env        = "${var.platform.name} ${var.platform.environment}"
    service                  = "DynamoDB"
    plat_env_project_service = "${var.platform.short_platform_name}_${upper(var.platform.prefix)}_DATA_DDB"
    managed_by               = "Terraform"
  }
}

resource "aws_dynamodb_table" "dynamodb-table-AppleTrackStream" {
  name         = "${local.name_prefix}-AppleTrackStream"
  billing_mode = "PAY_PER_REQUEST"
  hash_key     = var.tables_map.AppleTrackStream.partition_key
  range_key    = var.tables_map.AppleTrackStream.sorting_key

  dynamic "attribute" {
    for_each = var.tables_map.AppleTrackStream.attributes
    content {
      name = attribute.value.name
      type = attribute.value.type
    }
  }

  tags = merge(
    local.common_tags,
    {
      "Name" = "${local.name_prefix}-AppleTrackStream"
    },
  )
}

resource "aws_dynamodb_table" "dynamodb-table-AppleContainers" {
  name         = "${local.name_prefix}-AppleContainers"
  billing_mode = "PAY_PER_REQUEST"
  hash_key     = var.tables_map.AppleContainers.partition_key
  range_key    = var.tables_map.AppleContainers.sorting_key

  dynamic "attribute" {
    for_each = var.tables_map.AppleContainers.attributes
    content {
      name = attribute.value.name
      type = attribute.value.type
    }
  }

  tags = merge(
    local.common_tags,
    {
      "Name" = "${local.name_prefix}-AppleContainers"
    },
  )
}

resource "aws_dynamodb_table" "dynamodb-table-AppleTrackContainers" {
  name         = "${local.name_prefix}-AppleTrackContainers"
  billing_mode = "PAY_PER_REQUEST"
  hash_key     = var.tables_map.AppleTrackContainers.partition_key
  range_key    = var.tables_map.AppleTrackContainers.sorting_key

  dynamic "attribute" {
    for_each = var.tables_map.AppleTrackContainers.attributes
    content {
      name = attribute.value.name
      type = attribute.value.type
    }
  }

  tags = merge(
    local.common_tags,
    {
      "Name" = "${local.name_prefix}-AppleTrackContainers"
    },
  )
}

resource "aws_dynamodb_table" "dynamodb-table-AppleTrackDemographics" {
  name         = "${local.name_prefix}-AppleTrackDemographics"
  billing_mode = "PAY_PER_REQUEST"
  hash_key     = var.tables_map.AppleTrackDemographics.partition_key
  range_key    = var.tables_map.AppleTrackDemographics.sorting_key

  dynamic "attribute" {
    for_each = var.tables_map.AppleTrackDemographics.attributes
    content {
      name = attribute.value.name
      type = attribute.value.type
    }
  }

  tags = merge(
    local.common_tags,
    {
      "Name" = "${local.name_prefix}-AppleTrackDemographics"
    },
  )
}

resource "aws_dynamodb_table" "dynamodb-table-AppleSummary" {
  name         = "${local.name_prefix}-AppleSummary"
  billing_mode = "PAY_PER_REQUEST"
  hash_key     = var.tables_map.AppleSummary.partition_key
  range_key    = var.tables_map.AppleSummary.sorting_key

  dynamic "attribute" {
    for_each = var.tables_map.AppleSummary.attributes
    content {
      name = attribute.value.name
      type = attribute.value.type
    }
  }

  tags = merge(
    local.common_tags,
    {
      "Name" = "${local.name_prefix}-AppleSummary"
    },
  )
}

resource "aws_dynamodb_table" "dynamodb-table-AppleIngested" {
  name         = "${local.name_prefix}-AppleIngested"
  billing_mode = "PAY_PER_REQUEST"
  hash_key     = var.tables_map.AppleIngested.partition_key
  range_key    = var.tables_map.AppleIngested.sorting_key

  dynamic "attribute" {
    for_each = var.tables_map.AppleIngested.attributes
    content {
      name = attribute.value.name
      type = attribute.value.type
    }
  }

  tags = merge(
    local.common_tags,
    {
      "Name" = "${local.name_prefix}-AppleIngested"
    },
  )
}

resource "aws_dynamodb_table" "dynamodb-table-SpotifyTrackStream" {
  name         = "${local.name_prefix}-SpotifyTrackStream"
  billing_mode = "PAY_PER_REQUEST"
  hash_key     = var.tables_map.SpotifyTrackStream.partition_key
  range_key    = var.tables_map.SpotifyTrackStream.sorting_key

  dynamic "attribute" {
    for_each = var.tables_map.SpotifyTrackStream.attributes
    content {
      name = attribute.value.name
      type = attribute.value.type
    }
  }

  tags = merge(
    local.common_tags,
    {
      "Name" = "${local.name_prefix}-SpotifyTrackStream"
    },
  )
}

resource "aws_dynamodb_table" "dynamodb-table-SpotifyTrackPlaylistStream" {
  name         = "${local.name_prefix}-SpotifyTrackPlaylistStream"
  billing_mode = "PAY_PER_REQUEST"
  hash_key     = var.tables_map.SpotifyTrackPlaylistStream.partition_key
  range_key    = var.tables_map.SpotifyTrackPlaylistStream.sorting_key

  dynamic "attribute" {
    for_each = var.tables_map.SpotifyTrackPlaylistStream.attributes
    content {
      name = attribute.value.name
      type = attribute.value.type
    }
  }

  tags = merge(
    local.common_tags,
    {
      "Name" = "${local.name_prefix}-SpotifyTrackPlaylistStream"
    },
  )
}

resource "aws_dynamodb_table" "dynamodb-table-SpotifyPlaylistStream" {
  name         = "${local.name_prefix}-SpotifyPlaylistStream"
  billing_mode = "PAY_PER_REQUEST"
  hash_key     = var.tables_map.SpotifyPlaylistStream.partition_key
  range_key    = var.tables_map.SpotifyPlaylistStream.sorting_key

  dynamic "attribute" {
    for_each = var.tables_map.SpotifyPlaylistStream.attributes
    content {
      name = attribute.value.name
      type = attribute.value.type
    }
  }

  tags = merge(
    local.common_tags,
    {
      "Name" = "${local.name_prefix}-SpotifyPlaylistStream"
    },
  )
}

resource "aws_dynamodb_table" "dynamodb-table-databricks-SpotifyPlaylistStream" {
  name         = "${local.name_prefix}-databricks-SpotifyPlaylistStream"
  billing_mode = "PAY_PER_REQUEST"
  hash_key     = var.tables_map.SpotifyPlaylistStream.partition_key
  range_key    = var.tables_map.SpotifyPlaylistStream.sorting_key

  dynamic "attribute" {
    for_each = var.tables_map.SpotifyPlaylistStream.attributes
    content {
      name = attribute.value.name
      type = attribute.value.type
    }
  }

  tags = merge(
    local.common_tags,
    {
      "Name" = "${local.name_prefix}-databricks-SpotifyPlaylistStream"
    },
  )
}

resource "aws_dynamodb_table" "dynamodb-table-SpotifySummary" {
  name         = "${local.name_prefix}-SpotifySummary"
  billing_mode = "PAY_PER_REQUEST"
  hash_key     = var.tables_map.SpotifySummary.partition_key
  range_key    = var.tables_map.SpotifySummary.sorting_key

  dynamic "attribute" {
    for_each = var.tables_map.SpotifySummary.attributes
    content {
      name = attribute.value.name
      type = attribute.value.type
    }
  }

  tags = merge(
    local.common_tags,
    {
      "Name" = "${local.name_prefix}-SpotifySummary"
    },
  )
}

resource "aws_dynamodb_table" "dynamodb-table-SpotifyIngested" {
  name         = "${local.name_prefix}-SpotifyIngested"
  billing_mode = "PAY_PER_REQUEST"
  hash_key     = var.tables_map.SpotifyIngested.partition_key
  range_key    = var.tables_map.SpotifyIngested.sorting_key

  dynamic "attribute" {
    for_each = var.tables_map.SpotifyIngested.attributes
    content {
      name = attribute.value.name
      type = attribute.value.type
    }
  }

  tags = merge(
    local.common_tags,
    {
      "Name" = "${local.name_prefix}-SpotifyIngested"
    },
  )
}

resource "aws_dynamodb_table" "dynamodb-table-SpotifyReport" {
  name         = "${local.name_prefix}-SpotifyReport"
  billing_mode = "PAY_PER_REQUEST"
  hash_key     = var.tables_map.SpotifyReport.partition_key
  range_key    = var.tables_map.SpotifyReport.sorting_key

  dynamic "attribute" {
    for_each = var.tables_map.SpotifyReport.attributes
    content {
      name = attribute.value.name
      type = attribute.value.type
    }
  }

  tags = merge(
    local.common_tags,
    {
      "Name" = "${local.name_prefix}-SpotifyReport"
    },
  )
}
