{% for repo in resources -%}
resource "aws_ecr_repository" "repo_{{ repo.name_sanitized }}" {
  name = "{{ repo.repositoryName }}"

  image_tag_mutability = "{{ repo.imageTagMutability | upper }}"

  image_scanning_configuration {
    scan_on_push = {{ repo.image_scanning_enabled | lower }}
  }

  encryption_configuration {
    encryption_type = "{{ repo.encryption_config_formatted.encryption_type | upper }}"
    {%- if repo.encryption_config_formatted.get('kms_key') %}
    kms_key = "{{ repo.encryption_config_formatted.kms_key }}"
    {%- endif %}
  }

  tags = {
    {%- for key, value in repo.get('tags_formatted', {}).items() -%}
    "{{ key }}" = "{{ value }}"
    {%- endfor %}
  }
}

{%- if repo.get('repository_policy') %}
# Repository policy for {{ repo.repositoryName }}
resource "aws_ecr_repository_policy" "policy_{{ repo.name_sanitized }}" {
  repository = aws_ecr_repository.repo_{{ repo.name_sanitized }}.name
  policy     = jsonencode({{ repo.repository_policy | safe }})
}
{%- endif %}

{%- if repo.get('lifecycle_policy') %}
# Lifecycle policy for {{ repo.repositoryName }}
resource "aws_ecr_lifecycle_policy" "lifecycle_{{ repo.name_sanitized }}" {
  repository = aws_ecr_repository.repo_{{ repo.name_sanitized }}.name
  policy     = jsonencode({{ repo.lifecycle_policy | safe }})
}
{%- endif %}

{%- if not loop.last %}

{% endif %}
{%- endfor %}