<article class="card h-100 border-secondary-subtle shadow-sm">
    <div class="card-body d-flex flex-column gap-3">
        <div class="d-flex flex-wrap justify-content-between align-items-start gap-2">
            <div>
                <div class="d-flex flex-wrap align-items-center gap-2 mb-1">
                    <span class="badge bg-secondary">Case {{ linked_case.pk }}</span>
                    {% if is_current %}
                        <span class="badge bg-primary">Current</span>
                    {% endif %}
                    {% if linked_case.open_access %}
                        <span class="badge bg-success">Open access</span>
                    {% else %}
                        <span class="badge bg-warning text-dark">Restricted</span>
                    {% endif %}
                    {% if linked_case.verified %}
                        <span class="badge bg-info text-dark">Verified</span>
                    {% endif %}
                    {% if linked_case.archive %}
                        <span class="badge bg-dark">Archived</span>
                    {% endif %}
                </div>
                <h3 class="h5 mb-1">
                    <a class="link-body-emphasis text-decoration-none" href="{{ linked_case.get_absolute_url }}">
                        {{ linked_case.title|default:"Untitled case" }}
                    </a>
                </h3>
                {% if relation_label %}
                    <div class="small text-muted">{{ relation_label }}</div>
                {% endif %}
            </div>
            <div class="text-sm-end small text-muted">
                {% if relative_label %}
                    <div>Timeline {{ relative_label.text }}</div>
                {% elif linked_case.get_effective_study_date %}
                    <div>Study date {{ linked_case.get_effective_study_date|date:"Y-m-d" }}</div>
                {% elif linked_case.published_date %}
                    <div>Published {{ linked_case.published_date|date:"Y-m-d" }}</div>
                {% else %}
                    <div>Created {{ linked_case.created_date|date:"Y-m-d" }}</div>
                {% endif %}
                {% if linked_case.diagnostic_certainty %}
                    <div>Certainty: {{ linked_case.get_diagnostic_certainty_display }}</div>
                {% endif %}
            </div>
        </div>

        {% if linked_case.description %}
            <p class="mb-0 text-body-secondary">{{ linked_case.description|striptags|truncatechars:220 }}</p>
        {% elif linked_case.history %}
            <p class="mb-0 text-body-secondary">{{ linked_case.history|striptags|truncatechars:220 }}</p>
        {% endif %}

        <div class="d-flex flex-column gap-2 small">
            {% if linked_case.subspecialty.all %}
                <div>
                    <span class="text-muted me-2">Subspecialty</span>
                    {% for item in linked_case.subspecialty.all %}
                        <span class="badge text-bg-light border">{{ item }}</span>
                    {% empty %}{% endfor %}
                </div>
            {% endif %}
            {% if linked_case.condition.all %}
                <div>
                    <span class="text-muted me-2">Condition</span>
                    {% for item in linked_case.condition.all|slice:":4" %}
                        <span class="badge text-bg-light border">{{ item }}</span>
                    {% endfor %}
                    {% if linked_case.condition.count > 4 %}
                        <span class="text-muted">+{{ linked_case.condition.count|add:-4 }} more</span>
                    {% endif %}
                </div>
            {% endif %}
            {% if linked_case.presentation.all %}
                <div>
                    <span class="text-muted me-2">Presentation</span>
                    {% for item in linked_case.presentation.all|slice:":4" %}
                        <span class="badge text-bg-light border">{{ item }}</span>
                    {% endfor %}
                    {% if linked_case.presentation.count > 4 %}
                        <span class="text-muted">+{{ linked_case.presentation.count|add:-4 }} more</span>
                    {% endif %}
                </div>
            {% endif %}
        </div>

        <div class="border rounded p-3 bg-body-tertiary">
            <div class="d-flex justify-content-between align-items-center gap-2 mb-2">
                <strong class="small">Series</strong>
                <span class="badge bg-dark-subtle text-dark-emphasis">{{ linked_case.get_ordered_series|length }} linked</span>
            </div>
            {% with ordered_series=linked_case.get_ordered_series %}
                {% if ordered_series %}
                    <div class="d-flex flex-column gap-2 small">
                        {% for series in ordered_series|slice:":4" %}
                            <div class="d-flex justify-content-between align-items-start gap-2 border rounded px-2 py-1 bg-body">
                                <div>
                                    <a class="text-decoration-none" href="{{ series.get_absolute_url }}">
                                        {{ series.description|default:series.examination }}
                                    </a>
                                    <div class="text-muted">{{ series.modality }}{% if series.examination %} · {{ series.examination }}{% endif %}{% if series.plane %} · {{ series.plane }}{% endif %}</div>
                                </div>
                                <span class="badge text-bg-light border">#{{ forloop.counter }}</span>
                            </div>
                        {% endfor %}
                    </div>
                    {% if ordered_series|length > 4 %}
                        <div class="small text-muted mt-2">Showing 4 of {{ ordered_series|length }} linked series.</div>
                    {% endif %}
                {% else %}
                    <div class="small text-muted">No series linked to this case.</div>
                {% endif %}
            {% endwith %}
        </div>

        <div class="d-flex flex-wrap gap-2 mt-auto">
            <a class="btn btn-sm btn-primary" href="{{ linked_case.get_absolute_url }}">Open case</a>
            <a class="btn btn-sm btn-outline-secondary" href="{% url 'atlas:linked_cases_overview' linked_case.pk %}">Re-centre overview</a>
        </div>
    </div>
</article>