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

{% block content %}
    <div class="d-flex justify-content-between align-items-start mb-3">
        <h3 class="mb-0">{{ condition.name }}</h3>
        <div class="btn-group" role="group" aria-label="Condition actions">
            <a class="btn btn-sm btn-outline-primary" href="{% url 'atlas:condition_update' pk=condition.pk %}">Edit</a>
            <a class="btn btn-sm btn-outline-danger" href="{% url 'atlas:condition_delete' pk=condition.pk %}">Delete</a>
            {% if request.user.is_superuser %}
                <a class="btn btn-sm btn-outline-secondary" href="{% url 'admin:atlas_condition_change' condition.id %}">Admin</a>
            {% endif %}
        </div>
    </div>

    <div class="row">
        <div class="col-lg-8">
            <section class="mb-3">
                <h5 class="mb-2">Details</h5>
                <dl class="row">
                    <dt class="col-sm-4">Primary name</dt>
                    <dd class="col-sm-8">{% if condition.canonical %}No (alias of <a href="{{ condition.canonical.get_absolute_url }}">{{ condition.canonical.name }}</a>){% else %}Yes{% endif %}</dd>

                    <dt class="col-sm-4">Subspecialty</dt>
                    <dd class="col-sm-8">
                        {% for sub in condition.subspecialty.all %}
                            <a class="badge bg-dark text-white me-1 text-decoration-none" href="{{ sub.get_absolute_url }}">{{ sub }}</a>
                        {% empty %}
                            &mdash;
                        {% endfor %}
                    </dd>

                    <dt class="col-sm-4">Synonyms</dt>
                    <dd class="col-sm-8">
                        <div id="synonyms-container">
                            {% include 'atlas/partials/_synonyms_list.html' %}
                        </div>
                    </dd>

                    <dt class="col-sm-4">Parent</dt>
                    <dd class="col-sm-8">
                        {% for parent in condition.parent.all %}
                            <a class="badge bg-primary text-white text-decoration-none me-1" href="{{ parent.get_absolute_url }}">{{ parent }}</a>
                        {% empty %}
                            &mdash;
                        {% endfor %}
                    </dd>

                    <dt class="col-sm-4">Children</dt>
                    <dd class="col-sm-8">
                        {% for child in condition.child.all %}
                            <a class="badge bg-info text-dark text-decoration-none me-1" href="{{ child.get_absolute_url }}">{{ child }}</a>
                        {% empty %}
                            &mdash;
                        {% endfor %}
                    </dd>

                    {% if can_merge %}
                        <dt class="col-sm-4">Edit relationships</dt>
                        <dd class="col-sm-8">
                            <p>
                                <a class="btn btn-sm btn-outline-primary" data-bs-toggle="collapse" href="#relationshipCollapse" role="button" aria-expanded="false" aria-controls="relationshipCollapse">Show relationship editor</a>
                            </p>
                            <div class="collapse" id="relationshipCollapse">
                                <div class="card card-body">
                                    {% include 'atlas/partials/_condition_relationships.html' %}
                                </div>
                            </div>
                        </dd>
                    {% endif %}

                    <dt class="col-sm-4">RCR condition</dt>
                    <dd class="col-sm-8">{% if condition.rcr_curriculum %}Yes{% else %}No{% endif %}</dd>

                    {% if not condition.rcr_curriculum %}
                        <dt class="col-sm-4">RCR condition map</dt>
                        <dd class="col-sm-8">
                            {% for map in condition.rcr_curriculum_map.all %}
                                <a class="badge bg-success text-white text-decoration-none me-1" href="{{ map.get_absolute_url }}">{{ map }}</a>
                            {% empty %}
                                &mdash;
                            {% endfor %}
                        </dd>
                    {% endif %}
                </dl>
            </section>

            {% if condition.case_set.exists %}
                <div class="card mb-3">
                    <div class="card-body">
                        <h5 class="card-title">Associated Cases</h5>
                        <ul class="list-unstyled mb-0">
                            {% for case in condition.case_set.all %}
                                <li class="mb-1">{{ case.get_link }}</li>
                            {% endfor %}
                        </ul>
                    </div>
                </div>
            {% endif %}

            {# Also include cases that reference this condition via CaseDisplaySet #}
            {% if condition.casedisplayset_set.exists %}
                <div class="card mb-3">
                    <div class="card-body">
                        <h5 class="card-title">Cases via Display Sets</h5>
                        <ul class="list-unstyled mb-0">
                            {% for ds in condition.casedisplayset_set.all %}
                                <li class="mb-1">{{ ds.case.get_link }} <small class="text-muted">[Display set: {{ ds.name }}]</small></li>
                            {% endfor %}
                        </ul>
                    </div>
                </div>
            {% endif %}

            {% if condition.differential_set.exists %}
                <div class="card mb-3">
                    <div class="card-body">
                        <h5 class="card-title">Associated Case Differentials</h5>
                        <ul class="list-unstyled mb-0">
                            {% for diff in condition.differential_set.all %}
                                <li class="mb-1">{{ diff.case.get_link }} <small class="text-muted">[{{ diff.text }}]</small></li>
                            {% endfor %}
                        </ul>
                    </div>
                </div>
            {% endif %}

            {% if condition.sbas_questions.exists %}
                <div class="card mb-3">
                    <div class="card-body">
                        <h5 class="card-title">Related SBA Questions</h5>
                        <ul class="list-unstyled mb-0">
                            {% for q in condition.sbas_questions.all %}
                                <li class="mb-1"><a href="{{ q.get_absolute_url }}">{% if q.title %}{{ q.title }}{% else %}{{ q.get_stem_stripped|truncatechars:140 }}{% endif %}</a></li>
                            {% endfor %}
                        </ul>
                    </div>
                </div>
            {% endif %}

            {% if not condition.rcr_curriculum and can_merge %}
                <div class="card mb-3">
                    <div class="card-body">
                        <h5 class="card-title">Merge</h5>
                        <p class="small text-muted">Merge this condition into another. This will transfer associated cases and differentials but will not transfer synonyms, parents or children.</p>

                        <p>
                            <a class="btn btn-sm btn-outline-primary" data-bs-toggle="collapse" href="#mergeCollapse" role="button" aria-expanded="false" aria-controls="mergeCollapse">Show merge form</a>
                        </p>

                        <div class="collapse" id="mergeCollapse">
                            <div class="card card-body">
                                <form method="POST" id="condition-merge-form">
                                    {% csrf_token %}
                                    <div class="mb-3">
                                        {{ form.as_p }}
                                    </div>
                                    <button class="btn btn-danger"
                                            hx-post='{% url "atlas:condition_merge" condition.pk %}'
                                            hx-target="#merge-options" hx-swap="innerHTML"
                                            hx-confirm="This will merge {{condition.name}} into the selected condition, this cannot be undone. Continue?">
                                        Merge
                                    </button>
                                </form>
                                <div id="merge-options" class="mt-3"></div>
                            </div>
                        </div>
                    </div>
                </div>
            {% endif %}
        </div>

        <aside class="col-lg-4">
            <div class="card mb-3">
                <div class="card-body">
                    <h6 class="card-subtitle mb-2 text-muted">Quick facts</h6>
                    <p class="mb-0"><strong>Case count:</strong> {{ condition.case_set.count }}</p>
                </div>
            </div>
        </aside>
    </div>

{% endblock %}

{% block js %}

{% endblock %}
