{% set config = merged_configs.get('aws_dynamodb_table', {}) %}
{% set comments = compliance_comments.get('aws_dynamodb_table', []) %}
# ------------------------------------------------------------------------------
# DynamoDB Table
# ------------------------------------------------------------------------------
{% for comment in comments %}
# {{ comment }}
{% endfor %}

resource "aws_dynamodb_table" "this" {
  name         = "{{ config.get('name', project_id ~ '-table') }}"
  billing_mode = "{{ config.get('billing_mode', 'PAY_PER_REQUEST') }}"
  hash_key     = "{{ config.get('hash_key', 'id') }}"

  {% if config.get('range_key') %}
  range_key = "{{ config.range_key }}"
  {% endif %}

  attribute {
    name = "{{ config.get('hash_key', 'id') }}"
    type = "{{ config.get('hash_key_type', 'S') }}"
  }

  {% if config.get('range_key') %}
  attribute {
    name = "{{ config.range_key }}"
    type = "{{ config.get('range_key_type', 'S') }}"
  }
  {% endif %}

  # Deletion protection
  deletion_protection_enabled = {{ config.get('deletion_protection_enabled', true) | lower }}

  # Point-in-time recovery
  point_in_time_recovery {
    enabled = {{ config.get('point_in_time_recovery_enabled', true) | lower }}
  }

  # Encryption
  server_side_encryption {
    enabled     = {{ config.get('server_side_encryption_enabled', true) | lower }}
    kms_key_arn = {{ config.get('kms_key_arn', 'null') }}
  }

  tags = {
    Name      = "{{ config.get('name', project_id ~ '-table') }}"
    ManagedBy = "complyform"
  }
}
