resource "aws_dynamodb_table" "this" {
  count        = var.enabled ? 1 : 0
  name         = var.table_name
  billing_mode = var.billing_mode

  hash_key  = var.pk
  range_key = var.sk != "" ? var.sk : null

  attribute {
    name = var.pk
    type = "S"
  }

  dynamic "attribute" {
    for_each = var.sk != "" ? [var.sk] : []
    content {
      name = attribute.value
      type = "S"
    }
  }

  point_in_time_recovery {
    enabled = true
  }

  server_side_encryption {
    enabled = true
  }

  tags = {
    "nova:managed-by" = "platform"
    "nova:module"     = "dynamodb"
  }

  lifecycle {
    prevent_destroy = true
  }
}

variable "table_name" {
  type = string
}

variable "region" {
  type    = string
  default = "us-east-1"
}

variable "pk" {
  type = string
}

variable "sk" {
  type    = string
  default = ""
}

variable "billing_mode" {
  type    = string
  default = "PAY_PER_REQUEST"
}

variable "enabled" {
  type    = bool
  default = true
}

output "table_arn" {
  value = var.enabled ? aws_dynamodb_table.this[0].arn : ""
}

output "table_name" {
  value = var.enabled ? aws_dynamodb_table.this[0].name : ""
}
