{% extends 'atlas/base.html' %}

{% block content_container_class %}container-fluid content-wide px-3{% endblock %}

{% block content %}
    <h2>Search Uploaded And Imported DICOMs</h2>
    <p class="text-muted">Admin-only tool for searching pending uploads and imported Atlas data by hash, series UID, or study UID.</p>

    <form method="get" class="mb-3">
        <div class="row g-2">
            <div class="col-lg-4">
                <label class="form-label mb-1" for="hash-query-input">Hash</label>
                <input
                    id="hash-query-input"
                    type="text"
                    class="form-control"
                    name="hash"
                    value="{{ hash_query }}"
                    placeholder="Pixel-data hash (partial OK)"
                    autocomplete="off"
                    spellcheck="false"
                >
            </div>
            <div class="col-lg-4">
                <label class="form-label mb-1" for="series-query-input">Series UID</label>
                <input
                    id="series-query-input"
                    type="text"
                    class="form-control"
                    name="series"
                    value="{{ series_query }}"
                    placeholder="SeriesInstanceUID"
                    autocomplete="off"
                    spellcheck="false"
                >
            </div>
            <div class="col-lg-4">
                <label class="form-label mb-1" for="study-query-input">Study UID</label>
                <input
                    id="study-query-input"
                    type="text"
                    class="form-control"
                    name="study"
                    value="{{ study_query }}"
                    placeholder="StudyInstanceUID"
                    autocomplete="off"
                    spellcheck="false"
                >
            </div>
        </div>
        <div class="d-flex align-items-center gap-2 mt-2">
            <button type="submit" class="btn btn-primary">Search</button>
            <a href="{% url 'atlas:uploads_hash_search' %}" class="btn btn-outline-secondary">Clear</a>
        </div>
        <small class="form-text text-muted">All searches are case-insensitive and support partial matches. Study UID matching now uses indexed Atlas series data.</small>
    </form>

    {% if hash_query or series_query or study_query %}
        <p>
            Found
            <strong>{{ uploads_result_count }}</strong> pending upload{{ uploads_result_count|pluralize }}
            and
            <strong>{{ imported_result_count }}</strong> imported image{{ imported_result_count|pluralize }}
            and
            <strong>{{ matched_series_result_count }}</strong> existing Atlas series.
        </p>

        <h4>Existing Atlas series</h4>
        {% if matched_series_page_obj.object_list %}
            <div class="table-responsive">
                <table class="table table-sm table-striped align-middle">
                    <thead>
                        <tr>
                            <th>Series</th>
                            <th>Series UID</th>
                            <th>Cases</th>
                            <th>Links</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for series in matched_series_page_obj.object_list %}
                            <tr>
                                <td>{{ series }}</td>
                                <td><code>{{ series.series_instance_uid|default:'-' }}</code></td>
                                <td>{{ series.case.count }}</td>
                                <td>
                                    <a href="{% url 'atlas:series_detail' series.pk %}">View series</a>
                                </td>
                            </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>

            {% if matched_series_page_obj.paginator.num_pages > 1 %}
                <nav aria-label="Matched series pagination">
                    <ul class="pagination">
                        {% if matched_series_page_obj.has_previous %}
                            <li class="page-item">
                                <a class="page-link" href="?hash={{ hash_query|urlencode }}&series={{ series_query|urlencode }}&study={{ study_query|urlencode }}&series_page={{ matched_series_page_obj.previous_page_number }}&uploads_page={{ uploads_page_obj.number }}&imported_page={{ imported_page_obj.number }}">Previous</a>
                            </li>
                        {% else %}
                            <li class="page-item disabled"><span class="page-link">Previous</span></li>
                        {% endif %}

                        <li class="page-item disabled">
                            <span class="page-link">Page {{ matched_series_page_obj.number }} of {{ matched_series_page_obj.paginator.num_pages }}</span>
                        </li>

                        {% if matched_series_page_obj.has_next %}
                            <li class="page-item">
                                <a class="page-link" href="?hash={{ hash_query|urlencode }}&series={{ series_query|urlencode }}&study={{ study_query|urlencode }}&series_page={{ matched_series_page_obj.next_page_number }}&uploads_page={{ uploads_page_obj.number }}&imported_page={{ imported_page_obj.number }}">Next</a>
                            </li>
                        {% else %}
                            <li class="page-item disabled"><span class="page-link">Next</span></li>
                        {% endif %}
                    </ul>
                </nav>
            {% endif %}
        {% else %}
            <div class="alert alert-warning">No existing Atlas series matched the current search.</div>
        {% endif %}

        <h4>Pending uploads</h4>
        {% if uploads_page_obj.object_list %}
            <div class="table-responsive">
                <table class="table table-sm table-striped align-middle">
                    <thead>
                        <tr>
                            <th>Hash</th>
                            <th>Uploaded</th>
                            <th>User</th>
                            <th>Series UID</th>
                            <th>Links</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for dicom in uploads_page_obj.object_list %}
                            <tr>
                                <td><code>{{ dicom.image_blake3_hash }}</code></td>
                                <td>{{ dicom.created_date }}</td>
                                <td>{{ dicom.user }}</td>
                                <td><code>{{ dicom.series_instance_uid|default:"-" }}</code></td>
                                <td>
                                    {% if dicom.series_instance_uid %}
                                        <a href="{% url 'atlas:user_uploads_series' dicom.series_instance_uid %}">View series uploads</a>
                                    {% else %}
                                        -
                                    {% endif %}
                                </td>
                            </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>

            {% if uploads_page_obj.paginator.num_pages > 1 %}
                <nav aria-label="Pending uploads pagination">
                    <ul class="pagination">
                        {% if uploads_page_obj.has_previous %}
                            <li class="page-item">
                                <a class="page-link" href="?hash={{ hash_query|urlencode }}&series={{ series_query|urlencode }}&study={{ study_query|urlencode }}&uploads_page={{ uploads_page_obj.previous_page_number }}&imported_page={{ imported_page_obj.number }}&series_page={{ matched_series_page_obj.number }}">Previous</a>
                            </li>
                        {% else %}
                            <li class="page-item disabled"><span class="page-link">Previous</span></li>
                        {% endif %}

                        <li class="page-item disabled">
                            <span class="page-link">Page {{ uploads_page_obj.number }} of {{ uploads_page_obj.paginator.num_pages }}</span>
                        </li>

                        {% if uploads_page_obj.has_next %}
                            <li class="page-item">
                                <a class="page-link" href="?hash={{ hash_query|urlencode }}&series={{ series_query|urlencode }}&study={{ study_query|urlencode }}&uploads_page={{ uploads_page_obj.next_page_number }}&imported_page={{ imported_page_obj.number }}&series_page={{ matched_series_page_obj.number }}">Next</a>
                            </li>
                        {% else %}
                            <li class="page-item disabled"><span class="page-link">Next</span></li>
                        {% endif %}
                    </ul>
                </nav>
            {% endif %}
        {% else %}
            <div class="alert alert-warning">No pending uploads matched the current search.</div>
        {% endif %}

        <h4 class="mt-4">Imported Atlas images</h4>
        {% if imported_page_obj.object_list %}
            <div class="table-responsive">
                <table class="table table-sm table-striped align-middle">
                    <thead>
                        <tr>
                            <th>Hash</th>
                            <th>Image ID</th>
                            <th>Series</th>
                            <th>Series UID</th>
                            <th>Links</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for image in imported_page_obj.object_list %}
                            <tr>
                                <td><code>{{ image.image_blake3_hash }}</code></td>
                                <td>{{ image.pk }}</td>
                                <td>
                                    {% if image.series %}
                                        {{ image.series }}
                                    {% else %}
                                        -
                                    {% endif %}
                                </td>
                                <td>
                                    {% if image.series and image.series.series_instance_uid %}
                                        <code>{{ image.series.series_instance_uid }}</code>
                                    {% else %}
                                        -
                                    {% endif %}
                                </td>
                                <td>
                                    {% if image.series %}
                                        <a href="{% url 'atlas:series_detail' image.series.pk %}">View series</a>
                                    {% else %}
                                        -
                                    {% endif %}
                                </td>
                            </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>

            {% if imported_page_obj.paginator.num_pages > 1 %}
                <nav aria-label="Imported images pagination">
                    <ul class="pagination">
                        {% if imported_page_obj.has_previous %}
                            <li class="page-item">
                                <a class="page-link" href="?hash={{ hash_query|urlencode }}&series={{ series_query|urlencode }}&study={{ study_query|urlencode }}&uploads_page={{ uploads_page_obj.number }}&imported_page={{ imported_page_obj.previous_page_number }}&series_page={{ matched_series_page_obj.number }}">Previous</a>
                            </li>
                        {% else %}
                            <li class="page-item disabled"><span class="page-link">Previous</span></li>
                        {% endif %}

                        <li class="page-item disabled">
                            <span class="page-link">Page {{ imported_page_obj.number }} of {{ imported_page_obj.paginator.num_pages }}</span>
                        </li>

                        {% if imported_page_obj.has_next %}
                            <li class="page-item">
                                <a class="page-link" href="?hash={{ hash_query|urlencode }}&series={{ series_query|urlencode }}&study={{ study_query|urlencode }}&uploads_page={{ uploads_page_obj.number }}&imported_page={{ imported_page_obj.next_page_number }}&series_page={{ matched_series_page_obj.number }}">Next</a>
                            </li>
                        {% else %}
                            <li class="page-item disabled"><span class="page-link">Next</span></li>
                        {% endif %}
                    </ul>
                </nav>
            {% endif %}
        {% else %}
            <div class="alert alert-warning">No imported Atlas images matched the current search.</div>
        {% endif %}
    {% else %}
        <div class="alert alert-info">Enter a hash, series UID, or study UID to search uploads and existing imports.</div>
    {% endif %}
{% endblock %}
