{% from "macros/model-table.html" import model_table %}
{% from "macros/tag.html" import feed_link %}
{% macro dataset_row(dataset, current_account, config, header_start=1) %}
<div class="dataset" id="dataset-{{ dataset.slug }}">
    <div class="dataset-topmatter topmatter">
        <div class="dataset-header header">
            <h{{ header_start }} class="dataset-title">{{ dataset.title }} </h{{ header_start }}>
            {% if not dataset.is_approved %}
            <p class="unapproved">
                Dataset has not yet been reviewed, and is not visible in public lists
            </p>
            {% endif %}
            <span class="dataset-slug token"><a href="/datasets/{{dataset.slug}}">{{ dataset.slug }}</a></span>
            <p class="dataset-publisher">{{ dataset.publisher }}</p>
            {%- if dataset.homepage %}
            <a class="dataset-homepage" href="{{ dataset.homepage }}">{{ dataset.homepage }}</a>
            {% endif %}
            {% if dataset.tags %}
            <div class="tag-group">
                {% for tag in dataset.tags %}
                {{ feed_link(tag=tag.tag) }}
                {% endfor %}
            </div>
            {% endif %}
        </div>
        <div class="dataset-infobox infobox">
            <span class="dataset-threat-label">Threat</span>
            <p class="dataset-threat threat-{{ dataset.threat.value }}">{{dataset.threat.value}}</p>
            <table class="infobox-table">
                {% if dataset.account %}
                <tr>
                    <td class="infobox-key">Submitted By</td>
                    <td class="infobox-value">
                    <a class="account-link" href="/accounts/{{ dataset.account.username }}">
                        @{{ dataset.account.username }}
                    </a>
                    </td>
                </tr>
                {% endif %}
                <tr>
                    <td class="infobox-key">Scrape Status</td>
                    <td class="infobox-value">{{ dataset.scrape_status.value }}</td>
                </tr>
                <tr>
                    <td class="infobox-key">Available at Source</td>
                    <td class="infobox-value">{{ dataset.source_available }}</td>
                </tr>
                <tr>
                    <td class="infobox-key">Scarcity</td>
                    <td class="infobox-value">{{ dataset.scarcity }}</td>
                </tr>
                <tr>
                    <td class="infobox-key">Source Type</td>
                    <td class="infobox-value">{{ dataset.source_type }}</td>
                </tr>
                <tr>
                    <td class="infobox-key">Source Access</td>
                    <td class="infobox-value">{{ dataset.source_access }}</td>
                </tr>
                {% if dataset.external_identifiers %}
                {% for identifier in dataset.external_identifiers %}
                <tr>
                    <td class="infobox-key">{{ identifier.type }}</td>
                    <td class="infobox-value">{{ identifier.identifier }}</td>
                </tr>
                {% endfor %}
                {% endif %}
                {% if dataset.dataset_created_at %}
                <tr>
                    <td class="infobox-key">Dataset Created</td>
                    <td class="infobox-value">
                        <time datetime="{{ dataset.dataset_created_at.strftime('%y-%m-%d %H:%M') }}Z">
                        {{ dataset.dataset_created_at.strftime("%y-%m-%d %H:%M") }} UTC
                        </time>
                    </td>
                </tr>
                {% endif %}
                {% if dataset.dataset_updated_at %}
                <tr>
                    <td class="infobox-key">Dataset Last Updated</td>
                    <td class="infobox-value">
                        <time datetime="{{ dataset.dataset_updated_at.strftime('%y-%m-%d %H:%M') }}Z">
                            {{ dataset.dataset_updated_at.strftime("%y-%m-%d %H:%M") }} UTC
                        </time>
                    </td>
                </tr>
                {% endif %}
                {% if dataset.last_seen_at %}
                <tr>
                    <td class="infobox-key">
                        {%- if dataset.last_seen_at > now(UTC) %}
                            To be removed at
                        {%- else %}
                            Last Seen
                        {%- endif %}
                    </td>
                    <td class="infobox-value">{{ dataset.last_seen_at.strftime("%y-%m-%d %H:%M") }} UTC</td>
                </tr>
                {% endif %}
                <tr>
                    <td class="infobox-key">Submitted</td>
                    <td class="infobox-value">
                        <time datetime="{{ dataset.created_at.strftime('%y-%m-%d %H:%M') }}Z">
                            {{ dataset.created_at.strftime("%y-%m-%d %H:%M") }} UTC
                        </time>
                    </td>
                </tr>
                <tr>
                    <td class="infobox-key">Record Last Updated</td>
                    <td class="infobox-value">{{ dataset.updated_at.strftime("%y-%m-%d %H:%M") }} UTC</td>
                </tr>
                {% if current_account and current_account.has_scope("review") and not dataset.is_approved %}
                <tr>
                    <td colspan="2">
                        <p class="infobox-subhead font-mono">review status: unapproved</p>
                        <div class="infobox-row">{{ review_buttons(dataset, config) }}</div></td>
                </tr>
                {% endif %}
            </table>
        </div>
    </div>

    <div class="dataset-description">
    {{ dataset.description_html | safe }}
    </div>

    {% if dataset.urls | length > 0 %}
    <h{{ header_start + 1 }}>URLs</h{{ header_start + 1 }}>

    <ul class="url-list">
        {% for url in dataset.urls %}
        <li>
            <a href="{{ url.url }}">{{ url.url }}</a>
        </li>
        {% endfor %}
    </ul>
    {% endif %}

    <h{{ header_start + 1 }}>Uploads</h{{ header_start + 1 }}>

    {% if dataset.is_approved %}
    <div id="dataset-uploads-container" class="uploads-container container">
        <div
            hx-get="/datasets/{{ dataset.slug }}/uploads"
            hx-trigger="load"
        >
            <span class="htmx-indicator search-indicator" id="uploads-indicator">
                <span>Loading dataset uploads...</span>
                <img src="/static/img/rings.svg" alt="Loading indicator, a spinning ring"/>
            </span>
        </div>
    </div>
    {% endif %}

    <div class="upload-button">
        <button class="button" id="upload-button"
                hx-get="/datasets/{{dataset.slug}}/upload/start"
                hx-swap="outerHTML">
            Upload
        </button>
    </div>

    <h3>Parts</h3>
    <div id="dataset-{{ dataset.slug }}-parts-container" class="dataset-parts-container">
        <div
            hx-get="/datasets/{{ dataset.slug }}/parts"
            hx-trigger="load"
            hx-swap="outerHTML"
        >
            <span class="htmx-indicator search-indicator" id="uploads-indicator">
                <span>Loading dataset parts...</span>
                <img src="/static/img/rings.svg"/>
            </span>
        </div>
    </div>
</div>
{% endmacro %}

{% macro dataset_collapsible(dataset, review = False, config = None, types= None) %}
<details class="collapsible dataset-collapsible{% if not dataset.is_approved %} unapproved{% endif %}"
         id="dataset-collapsible-{{ dataset.slug }}">
    <summary class="collapsible-summary"
             hx-get="/datasets/{{ dataset.slug }}/partial"
             hx-trigger="click once"
             hx-target="#dataset-{{ dataset.slug }}">
        <span class="collapsible-marker" aria-label="Expand/Collapse"></span>
        <span class="dataset-slug item-link"><a href="/datasets/{{dataset.slug}}">{{ dataset.slug }}</a></span>
        <span class="dataset-title">{{ dataset.title }}</span>
        {% if dataset.threat != "unknown" %}
        <span class="dataset-threat threat-dot threat-{{ dataset.threat }}"
              title="{{ dataset.threat }}: {{ types.Threat.__annotations__[dataset.threat].__metadata__[-1] }}"
        ></span>
        {% else %}
            <span></span>
        {% endif %}
        <time class="dataset-timestamp timestamp" datetime="{{dataset.created_at.strftime('%y-%m-%d %H:%M') }}Z">
            {{ dataset.created_at.strftime("%y-%m-%d %H:%M") }}
        </time>
        {% if review %}
        {{ review_buttons(dataset, config) }}
        {% endif %}
    </summary>

    <div class="dataset" id="dataset-{{ dataset.slug }}"></div>
</details>
{% endmacro %}

{% macro review_buttons(dataset, config) %}
<button class="button approve-button success-button"
        role="button"
        aria-roledescription="Approves the item indicted by aria-controls, removing it from the list and making it visible on the site"
        aria-controls="dataset-collapsible-{{ dataset.slug }}"
        hx-post="{{ config.api_prefix }}/datasets/{{ dataset.slug }}/approve">Approve
</button>
<button class="button deny-button success-button caution"
        aria-roledescription="Denies the item indicted by aria-controls, removing it from the list and deleting it"
        aria-controls="dataset-collapsible-{{ dataset.slug }}"
        hx-post="{{ config.api_prefix }}/datasets/{{ dataset.slug }}/deny">Deny
</button>
{% endmacro %}
