{% from 'macros/common_macros.lua.j2' import string_table, boolean %}
{% from 'macros/policy_macros.lua.j2' import policy_get_tagset, policy_todname %}


{% macro local_data_root_fallback_addresses(pairs) -%}
hints.root({
{% for name, addresses in pairs.items() %}
  ['{{ name }}']={{ string_table(addresses) }},
{% endfor %}
})
{%- endmacro %}


{% macro local_data_root_fallback_addresses_files(files) -%}
{% for file in files %}
hints.root_file('{{ file }}')
{% endfor %}
{%- endmacro %}

{%- macro local_data_ttl(ttl) -%}
{%- if ttl -%}
{{ ttl.seconds() }}
{%- else -%}
{{ 'C.KR_RULE_TTL_DEFAULT' }}
{%- endif -%}
{%- endmacro -%}


{% macro kr_rule_local_address(name, address, nodata, ttl, tags=none) -%}
assert(C.kr_rule_local_address('{{ name }}', '{{ address }}',
    {{ boolean(nodata) }}, {{ local_data_ttl(ttl)}}, {{ policy_get_tagset(tags) }}) == 0)
{%- endmacro -%}


{% macro local_data_addresses(pairs, nodata, ttl) -%}
{% for name, addresses in pairs.items() %}
{% for address in addresses %}
{{ kr_rule_local_address(name, address, nodata, ttl) }}
{% endfor %}
{% endfor%}
{%- endmacro %}


{% macro kr_rule_local_hosts(file, nodata, ttl, tags=none) -%}
assert(C.kr_rule_local_hosts('{{ file }}', {{ boolean(nodata) }},
    {{ local_data_ttl(ttl)}}, {{ policy_get_tagset(tags) }}) == 0)
{%- endmacro %}


{% macro local_data_addresses_files(files, nodata, ttl, tags) -%}
{% for file in files %}
{{ kr_rule_local_hosts(file, nodata, ttl, tags) }}
{% endfor %}
{%- endmacro %}


{% macro local_data_records(input_str, is_rpz, nodata, ttl, tags=none, id='rrs') -%}
{{ id }} = ffi.new('struct kr_rule_zonefile_config')
{{ id }}.ttl = {{ local_data_ttl(ttl) }}
{{ id }}.tags = {{ policy_get_tagset(tags) }}
{{ id }}.nodata = {{ boolean(nodata) }}
{{ id }}.is_rpz = {{ boolean(is_rpz) }}
{% if is_rpz -%}
{{ id }}.filename = '{{ input_str }}'
{% else %}
{{ id }}.input_str = [[
{{ input_str.multiline() }}
]]
{% endif %}
assert(C.kr_rule_zonefile({{ id }})==0)
{%- endmacro %}


{% macro kr_rule_local_subtree(name, type, ttl, tags=none) -%}
assert(C.kr_rule_local_subtree(todname('{{ name }}'),
    C.KR_RULE_SUB_{{ type.upper() }}, {{ local_data_ttl(ttl) }}, {{ policy_get_tagset(tags) }}) == 0)
{%- endmacro %}


{% macro local_data_rules(items, nodata, ttl) -%}
{% for item in items %}
{% if item.name %}
{% for name in item.name %}
{% if item.address %}
{% for address in item.address %}
{{ kr_rule_local_address(name, address, nodata if item.nodata is none else item.nodata, item.ttl or ttl, item.tags) }}
{% endfor %}
{% endif %}
{% if item.subtree %}
{{ kr_rule_local_subtree(name, item.subtree, item.ttl or ttl, item.tags) }}
{% endif %}
{% endfor %}
{% elif item.file %}
{% for file in item.file %}
{{ kr_rule_local_hosts(file, nodata if item.nodata is none else item.nodata, item.ttl or ttl, item.tags) }}
{% endfor %}
{% elif item.records %}
{{ local_data_records(item.records, false, nodata if item.nodata is none else item.nodata, item.ttl or ttl, tags) }}
{% endif %}
{% endfor %}
{%- endmacro %}
