{% for record in resources %}
{% if record.get('name_sanitized') %}
{% set resource_name = record.name_sanitized %}
{% else %}
{% set zone_name = record.ZoneId | strip_id_prefix | terraform_name %}
{% set record_name = record.Name | terraform_name %}
{% set record_type = record.Type | lower %}
{% set resource_name = zone_name + "_" + record_name + "_" + record_type %}
{% endif %}
resource "aws_route53_record" "{{ resource_name }}" {
  zone_id = "{{ record.ZoneId }}"
  name    = "{{ record.Name }}"
  type    = "{{ record.Type }}"

  {% if not record.alias_config and record.get('TTL') is defined and record.TTL is not none %}
  ttl     = {{ record.TTL | safe_int }}
  {% endif %}

  {% if not record.alias_config %}
  {% if record.get('ResourceRecords') and record.ResourceRecords | has_value %}
  records = [
    {% for rec_value in record.ResourceRecords %}
    {% if record.Type == 'TXT' %}
    {{ rec_value.Value | quote_txt_value }}{{ "," if not loop.last }}
    {% else %}
    {{ rec_value.Value | tojson }}{{ "," if not loop.last }}
    {% endif %}
    {% endfor %}
  ]
  {% else %}
  # WARNING: No records found for this record. This might be an alias record.
  # Please check the AWS console and update accordingly.
  records = ["PLACEHOLDER_RECORD"]
  {% endif %}
  {% endif %}

  {% if record.alias_config %}
  {{ record.alias_config }}
  {% endif %}

  {% if record.get('SetIdentifier') and record.SetIdentifier | has_value %}
  set_identifier = "{{ record.SetIdentifier }}"
  {% endif %}

  {% if record.get('Weight') is defined and record.Weight is not none %}
  weighted_routing_policy {
    weight = {{ record.Weight | safe_int }}
  }
  {% endif %}

  {% if record.get('Region') and record.Region | has_value %}
  latency_routing_policy {
    region = "{{ record.Region }}"
  }
  {% endif %}

  {% if record.get('GeoLocation') %}
  geolocation_routing_policy {
    {% if record.GeoLocation.get('ContinentCode') %}
    continent = "{{ record.GeoLocation.ContinentCode }}"
    {% endif %}
    {% if record.GeoLocation.get('CountryCode') %}
    country = "{{ record.GeoLocation.CountryCode }}"
    {% endif %}
    {% if record.GeoLocation.get('SubdivisionCode') %}
    subdivision = "{{ record.GeoLocation.SubdivisionCode }}"
    {% endif %}
  }
  {% endif %}

  {% if record.get('Failover') and record.Failover | has_value %}
  failover_routing_policy {
    type = "{{ record.Failover }}"
  }
  {% endif %}

  {% if record.get('MultiValueAnswer') is defined %}
  multivalue_answer_routing_policy = {{ record.MultiValueAnswer | lower }}
  {% endif %}

  {% if record.get('HealthCheckId') and record.HealthCheckId | has_value %}
  health_check_id = "{{ record.HealthCheckId }}"
  {% endif %}
}

{% endfor %}
