{% import 'macros.txt' as macros %}
schema {{ schema_name }}{% if schema.inherits %} inherits {{ schema.inherits }}{% endif %} {
{% if stemming %}
    stemming: {{ stemming }}
{% endif %}
    document {{ document_name }}{% if document.inherits %} inherits {{ document.inherits }}{% endif %} {
    {% for field in document.fields %}
        {% if field.is_document_field %}
{{ macros.field(field, 8)[1:] }}
        {% endif %}
        {% endfor %}
        {% for struct in document.structs %}
        struct {{struct.name}} {
            {% for field in struct.fields %}
{{ macros.field(field, 12)[1:] }}
            {% endfor %}
        }
        {% endfor %}
    }
{% for field in document.fields %}
{% if not field.is_document_field %}
{{ macros.field(field, 4)[1:] }}
{% endif %}
{% endfor %}
{% for key, value in imported_fields.items() %}
    import field {{ value.reference_field }}.{{ value.field_to_import }} as {{ key }} {}
{% endfor %}
{% for key, value in fieldsets.items() %}
    fieldset {{ key }} {
        fields: {{ value.fields_to_text }}
    }
{% endfor %}
{% if models %}
{% for model in models %}
    onnx-model {{ model.model_name }} {
        file: {{ model.file_path }}
        {% for key_input, value_input in model.inputs.items() %}
        input {{ key_input }}: {{ value_input }}
        {% endfor %}
        {% for key_output, value_output in model.outputs.items() %}
        output {{ key_output }}: {{ value_output }}
        {% endfor %}
    }
{% endfor %}
{% endif %}
{% for key, value in rank_profiles.items() %}
    rank-profile {{ key }}{% if value.inherits %} inherits {{ value.inherits }}{% endif %} {
        {% if value.filter_threshold %}
        filter-threshold: {{ value.filter_threshold }}
        {% endif %}
        {% if value.weakand %}
        weakand {
            {% for key, val in value.weakand.items() %}
            {{ key }}: {{ val }}
            {% endfor %}
        }
        {% endif %}
        {% if value.constants %}
        constants {
        {% for constant_name, constant_value in value.constants.items() %}
            {{ constant_name }}: {{ constant_value }}
        {% endfor %}
        }
        {% endif %}
        {% if value.inputs %}
        inputs {
        {% for input in value.inputs %}
            {{ input.0 }} {{ input.1 }}{% if input|length > 2 %}: {{ input.2 }}{% endif %}{% raw %}
{% endraw %}
        {% endfor %}
        }
        {% endif %}
        {% if value.functions %}
        {% for function in value.functions %}
        function {{ function.name }}({{ function.args_to_text }}) {
            expression {
                {{ function.expression | indent( width=16, first=False) }}
            }
        }
        {% endfor %}
        {% endif %}
        {% if value.diversity %}
        diversity {
            attribute: {{ value.diversity.attribute }}
            min-groups: {{ value.diversity.min_groups }}
        }
        {% endif %}
        {% if value.match_phase %}
        match-phase {
            attribute: {{ value.match_phase.attribute }}
            order: {{ value.match_phase.order }}
            max-hits: {{ value.match_phase.max_hits }}
        }
        {% endif %}
        {% if value.first_phase %}
        {% if value.first_phase is string %}
        first-phase {
            expression {
                {{ value.first_phase }}
            }
        }
        {% else %}
        first-phase {
            expression {
                {{ value.first_phase.expression }}
            }
            {% if value.first_phase.keep_rank_count %}
            keep-rank-count: {{ value.first_phase.keep_rank_count }}
            {% endif %}
            {% if value.first_phase.rank_score_drop_limit != None %}
            rank-score-drop-limit: {{ value.first_phase.rank_score_drop_limit }}
            {% endif %}
        }
        {% endif %}
        {% endif %}
        {% if value.second_phase %}
        second-phase {
            expression {
                {{ value.second_phase.expression }}
            }
            rerank-count: {{ value.second_phase.rerank_count }}
            {% if value.second_phase.rank_score_drop_limit != None %}
            rank-score-drop-limit: {{ value.second_phase.rank_score_drop_limit }}
            {% endif %}
        }
        {% endif %}
        {% if value.global_phase %}
        global-phase {
            expression {
                {{ value.global_phase.expression }}
            }
            rerank-count: {{ value.global_phase.rerank_count }}
            {% if value.global_phase.rank_score_drop_limit != None %}
            rank-score-drop-limit: {{ value.global_phase.rank_score_drop_limit }}
            {% endif %}
        }
        {% endif %}
        {% if value.summary_features %}
        summary-features {
        {% for feature in value.summary_features %}
            {{ feature }}
        {% endfor %}
        }
        {% endif %}
        {% if value.match_features %}
        match-features {
        {% for feature in value.match_features %}
            {{ feature }}
        {% endfor %}
        }
        {% endif %}
        {% if value.weight %}
        {% for weight in value.weight %}
        weight {{ weight.0 }}: {{ weight.1 }}
        {% endfor %}
        {% endif %}
        {% if value.rank_type %}
        {% for rank_type in value.rank_type %}
        rank-type {{ rank_type.0 }}: {{ rank_type.1 }}
        {% endfor %}
        {% endif %}
        {% if value.rank_properties %}
        rank-properties {
            {% for rank_property in value.rank_properties %}
            {{ rank_property.0 }}: {% if rank_property.1 is number %}{{ rank_property.1 }}{% else %}"{{ rank_property.1 }}"{% endif %}{% raw %}
{% endraw %}
            {% endfor %}
        }
        {% endif %}
        {% if value.num_threads_per_search %}
        num-threads-per-search: {{ value.num_threads_per_search }}
        {% endif %}
        {% if value.mutate %}
        mutate {
            {% if value.mutate.on_match %}
            on-match    { {{ value.mutate.on_match_attribute }}   {{ value.mutate.on_match_operation_string }} {{ value.mutate.on_match_operation_value}} }
            {% endif %}
            {% if value.mutate.on_first_phase %}
            on-first-phase  { {{ value.mutate.on_first_phase_attribute }}   {{ value.mutate.on_first_phase_operation_string }} {{ value.mutate.on_first_phase_operation_value }} }
            {% endif %}
            {% if value.mutate.on_second_phase %}
            on-second-phase  { {{ value.mutate.on_second_phase_attribute }}   {{ value.mutate.on_second_phase_operation_string }} {{ value.mutate.on_second_phase_operation_value }} }
            {% endif %}
            {% if value.mutate.on_summary %}
            on-summary  { {{ value.mutate.on_summary_attribute }}   {{ value.mutate.on_summary_operation_string }} {{ value.mutate.on_summary_operation_value }} }
            {% endif %}
        }
        {% endif %}
    }
{% endfor %}
{% for document_summary in document_summaries %}
    document-summary {{ document_summary.name }}{% if document_summary.inherits %} inherits {{ document_summary.inherits }}{% endif %} {
        {% if document_summary.summary_fields %}
        {% for summary in document_summary.summary_fields %}
        {% for field in summary.as_lines %}
        {{ field }}
        {% endfor %}
        {% endfor %}
        {% endif %}
        {% if document_summary.from_disk %}
        from-disk
        {% endif %}
        {% if document_summary.omit_summary_features %}
        omit-summary-features
        {% endif %}
    }
{% endfor %}
}
