
{% macro fasta_check_report(validation_results, file_name) -%}
    {% set results_for_fasta = validation_results['fasta_check'][file_name] %}

    {% if results_for_fasta.get('connection_error') %}
        <div class="report-section fail">
            <strong>Warning:</strong> The following results may be incomplete due to problems with external services. Please try again later for
            complete results.
            <br><strong>Error message:</strong> {{ results_for_fasta.get('connection_error') }}
        </div>
    {% endif %}

    <!-- All INSDC check results -->
    {% set expand_icon = "" %}
    {% if results_for_fasta.get('all_insdc') %}
        {% set icon = "&#10004;" %}
        {% set row_class = "report-section pass" %}
        {% set text = "All sequences are INSDC accessioned" %}
    {% else %}
        {% set expand_icon = "&#9654;" %}
        {% set icon = "&#10060;" %}
        {% set row_class = "report-section fail collapsible" %}
        {% set text = "Some sequences are not INSDC accessioned" %}
    {% endif %}
    <div class='{{ row_class }}'><span class="expand_icon">{{ expand_icon }}</span> {{ icon }} {{ text }} </div>
    {% if not results_for_fasta.get('all_insdc') %}
        <div class="error-list">
            <div class="error-description">First 10 sequences not in INSDC. <strong>Full report:</strong> {{ results_for_fasta.get('report_path', '') }}</div>
            <table>
                <tr>
                    <th>Sequence name</th><th>Refget md5</th>
                </tr>
                {% set sequence_info_list = results_for_fasta.get('sequences', [])|rejectattr("insdc")|list %}
                {% for sequence_info in sequence_info_list[:10] %}
                    <tr>
                        <td><strong>{{sequence_info.get('sequence_name') }}</strong></td><td> {{ sequence_info.get('sequence_md5') }}</td>
                    </tr>
                {% endfor %}
            </table>
        </div>
    {% endif %}

    <!-- INSDC concordance check results (optional) -->
    {% if 'possible_assemblies' in results_for_fasta %}
        {% if 'metadata_assembly_compatible' in results_for_fasta %}
            <!-- found assembly in metadata, so definitely know the analysis -->
            {% set analysis_text = results_for_fasta.get('associated_analyses')|join(", ") %}
            {% set expand_icon = "" %}
            {% if results_for_fasta.get('metadata_assembly_compatible') %}
                {% set icon = "&#10004;" %}
                {% set row_class = "report-section pass" %}
                {% set text = analysis_text + ": Assembly accession in metadata is compatible" %}
            {% else %}
                {% set expand_icon = "&#9654;" %}
                {% set icon = "&#10060;" %}
                {% set row_class = "report-section fail collapsible" %}
                {% set text = analysis_text + ": Assembly accession in metadata is not compatible" %}
            {% endif %}
        {% else %}
            <!-- has possible assemblies but no metadata assembly -->
            {% set expand_icon = "&#9654;" %}
            {% set icon = "&#10060;" %}
            {% set row_class = "report-section fail collapsible" %}
            {% set text = "No assembly accession found in metadata" %}
        {% endif %}
        <div class='{{ row_class }}'><span class="expand_icon">{{ expand_icon }}</span> {{ icon }} {{ text }} </div>
        {% if 'metadata_assembly_compatible' not in results_for_fasta or not results_for_fasta['metadata_assembly_compatible'] %}
            <div class="error-list">
                <div class="error-description"><strong>Full report:</strong> {{ results_for_fasta.get('report_path', '') }}</div>
                <table>
                    <tr>
                        <th>Category</th><th>Accessions</th>
                    </tr>
                    <tr>
                        <td><strong>Assembly accession found in metadata</strong></td>
                        <td>{{ results_for_fasta.get('assembly_in_metadata', 'Not found') }}</td>
                    </tr>
                    <tr>
                        <td><strong>Assembly accession(s) compatible with FASTA</strong></td>
                        <td>{{ results_for_fasta.get('possible_assemblies')|join(", ") }}</td>
                    </tr>
                </table>
            </div>
        {% endif %}
    {% endif %}
{%- endmacro %}