{% extends "aws/base_resource.tf.j2" %}
{% block resources %}
{% for stream in resources %}
{{ resource_spacer() }}
resource "aws_kinesis_stream" "{{ stream.name_sanitized }}" {
  name = "{{ stream.get('StreamName', stream.get('streamName', '')) }}"

  {% if stream.get('shard_count') %}
  shard_count = {{ stream.shard_count }}
  {% elif stream.get('StreamModeDetails', {}).get('StreamMode') != 'ON_DEMAND' %}
  shard_count = {{ stream.get('Shards', [])|length | default(1) }}
  {% endif %}

  {% if stream.get('retention_period_hours') %}
  retention_period = {{ stream.retention_period_hours }}
  {% elif stream.get('RetentionPeriodHours') %}
  retention_period = {{ stream.RetentionPeriodHours }}
  {% endif %}

  {% if stream.get('encryption_type') %}
  encryption_type = "{{ stream.encryption_type }}"
  {% elif stream.get('EncryptionType') %}
  encryption_type = "{{ stream.EncryptionType }}"
  {% endif %}

  {% if stream.get('kms_key_id') %}
  kms_key_id = "{{ stream.kms_key_id }}"
  {% elif stream.get('KeyId') %}
  kms_key_id = "{{ stream.KeyId }}"
  {% endif %}

  {% if stream.get('shard_level_metrics') %}
  shard_level_metrics = {{ stream.shard_level_metrics | tojson }}
  {% elif stream.get('EnhancedMonitoring') %}
  shard_level_metrics = [
    {% for metric in stream.EnhancedMonitoring %}
    {% for shard_metric in metric.get('ShardLevelMetrics', []) %}
    "{{ shard_metric }}"{{ "," if not loop.last }}
    {% endfor %}
    {% endfor %}
  ]
  {% endif %}

  {% if stream.get('stream_mode_details') %}
  stream_mode_details {
    stream_mode = "{{ stream.stream_mode_details.stream_mode }}"
  }
  {% elif stream.get('StreamModeDetails') %}
  stream_mode_details {
    stream_mode = "{{ stream.StreamModeDetails.StreamMode }}"
  }
  {% endif %}

  {% if stream.get('enforce_consumer_deletion') is defined %}
  enforce_consumer_deletion = {{ stream.enforce_consumer_deletion | lower }}
  {% endif %}

  {{ render_common_tags(stream, indent=2) }}

  {% if stream.get('lifecycle_prevent_destroy', true) %}
  lifecycle {
    prevent_destroy = true
  }
  {% endif %}
}

{% endfor %}
{% endblock %}
