{% load django_htmx %}
{% comment %} Sharing panel for a CidUserExam attempt.
   Context:
     cid_user_exam  – CidUserExam instance
     sharing_info   – dict from cid_user_exam.get_sharing_info()
     is_owner       – bool: current user is the attempt owner
{% endcomment %}
<div id="sharing-panel-{{ cid_user_exam.pk }}" class="card border-secondary mt-3">
    <div class="card-header py-2 px-3 d-flex justify-content-between align-items-center">
        <span class="fw-semibold small">Result Sharing</span>
        <a href="{% url 'atlas:collection_supervision' %}" class="btn btn-sm btn-outline-secondary">Supervision</a>
    </div>
    <div class="card-body py-2 px-3">

        {# Always-visible users #}
        <div class="mb-3">
            <div class="small text-muted mb-1">Always shared with (authors &amp; markers)</div>
            {% if sharing_info.always_visible_to %}
                <ul class="list-unstyled mb-0">
                    {% for u in sharing_info.always_visible_to %}
                        <li class="small">
                            <span class="badge bg-secondary me-1">Author/Marker</span>
                            {{ u.get_full_name|default:u.username }}
                            {% if u.email %}<span class="text-muted">&lt;{{ u.email }}&gt;</span>{% endif %}
                        </li>
                    {% endfor %}
                </ul>
            {% else %}
                <span class="small text-muted fst-italic">No authors or markers assigned.</span>
            {% endif %}
        </div>

        {# Supervisor sharing #}
        {% if not sharing_info.exam_supervisor_visible %}
            <div class="mb-3">
                <div class="small text-muted mb-1">Share with supervisor</div>
                {% if is_owner %}
                    <button class="btn btn-sm {% if sharing_info.supervisor_enabled %}btn-success{% else %}btn-outline-secondary{% endif %}"
                            hx-post="{% url 'atlas:collection_exam_sharing_panel' cid_user_exam.pk %}"
                            hx-vals='{"action": "toggle_supervisor"}'
                            hx-target="#sharing-panel-{{ cid_user_exam.pk }}"
                            hx-swap="outerHTML">
                        {% if sharing_info.supervisor_enabled %}Supervisor sharing ON{% else %}Supervisor sharing OFF{% endif %}
                    </button>
                {% else %}
                    <span class="badge {% if sharing_info.supervisor_enabled %}bg-success{% else %}bg-secondary{% endif %}">
                        {% if sharing_info.supervisor_enabled %}ON{% else %}OFF{% endif %}
                    </span>
                {% endif %}
                {% if sharing_info.supervisor %}
                    <div class="small text-muted mt-1">
                        Supervisor:
                        <strong>{{ sharing_info.supervisor.get_full_name|default:sharing_info.supervisor.username }}</strong>
                        {% if sharing_info.supervisor.email %}&lt;{{ sharing_info.supervisor.email }}&gt;{% endif %}
                    </div>
                {% elif sharing_info.supervisor_enabled %}
                    <div class="small text-warning mt-1">No supervisor account linked to your profile — results cannot be shared automatically.</div>
                {% endif %}
            </div>
        {% else %}
            <div class="mb-3">
                <div class="small text-muted mb-1">Supervisor</div>
                <span class="badge bg-success">Sharing always enabled (exam setting)</span>
                {% if sharing_info.supervisor %}
                    <div class="small text-muted mt-1">
                        Supervisor:
                        <strong>{{ sharing_info.supervisor.get_full_name|default:sharing_info.supervisor.username }}</strong>
                        {% if sharing_info.supervisor.email %}&lt;{{ sharing_info.supervisor.email }}&gt;{% endif %}
                    </div>
                {% endif %}
            </div>
        {% endif %}

        {# Manual shares — only for registered-user attempts #}
        {% if cid_user_exam.user_user_id %}
            <div>
                <div class="small text-muted mb-1">Also shared with</div>
                {% if sharing_info.manual_shares %}
                    <ul class="list-unstyled mb-2">
                        {% for u in sharing_info.manual_shares %}
                            <li class="small d-flex align-items-center gap-2 mb-1">
                                <span>{{ u.get_full_name|default:u.username }}
                                    {% if u.email %}<span class="text-muted">&lt;{{ u.email }}&gt;</span>{% endif %}
                                </span>
                                {% if is_owner %}
                                    <button class="btn btn-sm btn-outline-danger py-0 px-1"
                                            hx-post="{% url 'atlas:collection_exam_sharing_panel' cid_user_exam.pk %}"
                                            hx-vals='{"action": "remove_user", "user_id": "{{ u.pk }}"}'
                                            hx-target="#sharing-panel-{{ cid_user_exam.pk }}"
                                            hx-swap="outerHTML">
                                        &times; Remove
                                    </button>
                                {% endif %}
                            </li>
                        {% endfor %}
                    </ul>
                {% else %}
                    <div class="small text-muted fst-italic mb-2">No additional users.</div>
                {% endif %}

                {% if is_owner %}
                    {# User search input #}
                    <div id="sharing-user-search-{{ cid_user_exam.pk }}">
                        <input type="text"
                               class="form-control form-control-sm"
                               placeholder="Search user by name or email…"
                               name="q"
                               autocomplete="off"
                               hx-get="{% url 'atlas:collection_exam_user_search' cid_user_exam.pk %}"
                               hx-trigger="input delay:300ms, focus"
                               hx-target="#sharing-search-results-{{ cid_user_exam.pk }}"
                               hx-swap="innerHTML">
                        <div id="sharing-search-results-{{ cid_user_exam.pk }}" class="mt-1"></div>
                    </div>
                {% endif %}
            </div>
        {% endif %}

    </div>
</div>
