{% extends 'base.html' %}
{% load auth_extras %}

{% block content %}
    <div class="container py-4">
        <div class="row">
            <div class="col-md-4 mb-3">
                <div class="card">
                    <div class="card-body text-center">
                        <div class="profile-avatar mb-3">
                            <div class="rounded-circle bg-secondary text-white d-inline-flex align-items-center justify-content-center" style="width:96px;height:96px;font-size:32px;">{{ user.first_name|default:user.username|slice:":1"|upper }}</div>
                        </div>
                        <h4 class="card-title mb-1">{{ user.get_full_name|default:user.username }}</h4>
                        <p class="text-muted mb-1">@<span id="username">{{ user.username }}</span></p>
                        <p class="mb-0"><a href="mailto:{{ user.email }}" id="email">{{ user.email }}</a></p>
                        <div class="mt-2">
                            <small class="text-muted">Permission groups:</small>
                            <div id="auth-groups-{{ user.pk }}" class="mt-1 d-flex flex-wrap gap-2 align-items-center">
                                {% if user.groups.all %}
                                    {% for perm_group in user.groups.all %}
                                        {% if request.user.is_superuser or is_cid_user_manager %}
                                            <button
                                                class="btn btn-sm btn-info text-dark"
                                                hx-post="{% url 'generic:user_toggle_group' user.pk perm_group.pk %}?type=auth"
                                                hx-target="#auth-groups-{{ user.pk }}"
                                                hx-swap="outerHTML"
                                            >
                                                {{ perm_group.name }}
                                                <span class="ms-1 text-muted small">&times;</span>
                                            </button>
                                        {% else %}
                                            <span class="badge bg-info text-dark me-1">{{ perm_group.name }}</span>
                                        {% endif %}
                                    {% endfor %}
                                {% else %}
                                    <span class="badge bg-light text-muted">No permission groups</span>
                                {% endif %}

                                {% if request.user.is_superuser or is_cid_user_manager %}
                                    <div class="dropdown">
                                        <button class="btn btn-sm btn-outline-primary dropdown-toggle" type="button" id="addAuthGroupDropdown" data-bs-toggle="dropdown" aria-expanded="false">
                                            Add
                                        </button>
                                        <ul class="dropdown-menu" aria-labelledby="addAuthGroupDropdown">
                                            {% if available_auth_groups.count %}
                                                {% for ag in available_auth_groups %}
                                                    <li>
                                                        <button
                                                            class="dropdown-item"
                                                            hx-post="{% url 'generic:user_toggle_group' user.pk ag.pk %}?type=auth"
                                                            hx-target="#auth-groups-{{ user.pk }}"
                                                            hx-swap="outerHTML"
                                                        >
                                                            {{ ag.name }}
                                                        </button>
                                                    </li>
                                                {% endfor %}
                                            {% else %}
                                                {% if request.user.is_superuser %}
                                                    <li>
                                                        <a class="dropdown-item" href="{% url 'admin:auth_group_add' %}" target="_blank" rel="noopener">Create permission group</a>
                                                    </li>
                                                {% else %}
                                                    <li><span class="dropdown-item disabled">No groups available to add</span></li>
                                                {% endif %}
                                            {% endif %}
                                        </ul>
                                    </div>
                                {% endif %}
                            </div>
                        </div>
                    </div>
                    <ul class="list-group list-group-flush">
                        <li class="list-group-item">Grade: <strong>{{ user.userprofile.grade|default:"N/A" }}</strong></li>
                        <li class="list-group-item">Registration: <strong>{{ user.userprofile.registration_number|default:"N/A" }}</strong></li>
                        <li class="list-group-item">Supervisor:
                            {% if user.userprofile.supervisor %}
                                {{ user.userprofile.supervisor }}
                                {% if request.user.is_superuser %}
                                    <a href="{% url 'generic:supervisor_detail' user.userprofile.supervisor.pk %}" class="ms-2"><i class="bi bi-box-arrow-up-right"></i></a>
                                {% endif %}
                            {% else %}
                                <span class="text-muted">None</span>
                            {% endif %}
                        </li>
                        <li class="list-group-item">Supervisor account:
                            {% if supervisor_account %}
                                <a href="{% url 'generic:supervisor_detail' supervisor_account.pk %}">{{ supervisor_account.name }}</a>
                            {% else %}
                                <span class="text-muted">Not linked</span>
                            {% endif %}
                        </li>
                    </ul>
                    <div class="card-body">
                        {% if supervisor_action == 'created' %}
                            <div class="alert alert-success py-2" role="alert">Supervisor account created and linked.</div>
                        {% elif supervisor_action == 'linked' %}
                            <div class="alert alert-success py-2" role="alert">Existing supervisor record linked to this user.</div>
                        {% elif supervisor_action == 'already_linked' %}
                            <div class="alert alert-info py-2" role="alert">This user is already linked to a supervisor account.</div>
                        {% elif supervisor_action == 'no_email' %}
                            <div class="alert alert-warning py-2" role="alert">Cannot create supervisor account: user has no email address.</div>
                        {% elif supervisor_action == 'email_in_use' %}
                            <div class="alert alert-danger py-2" role="alert">Cannot link supervisor account: email is already linked to another user.</div>
                        {% endif %}

                        {% if password_reset_action == 'sent' %}
                            <div class="alert alert-success py-2" role="alert">Password reset email sent.</div>
                        {% elif password_reset_action == 'no_email' %}
                            <div class="alert alert-warning py-2" role="alert">Cannot send password reset: user has no email address.</div>
                        {% elif password_reset_action == 'inactive' %}
                            <div class="alert alert-warning py-2" role="alert">Cannot send password reset: user account is inactive.</div>
                        {% elif password_reset_action == 'invalid' %}
                            <div class="alert alert-danger py-2" role="alert">Cannot send password reset: email address is invalid.</div>
                        {% elif password_reset_action == 'error' %}
                            <div class="alert alert-danger py-2" role="alert">Password reset email could not be sent.</div>
                        {% endif %}

                        <a class="btn btn-outline-primary d-block mb-2" href="{% url 'password_change' %}" id="password-change">Change password</a>
                        {% if request.user|has_group:"cid_user_manager" %}
                            <a class="btn btn-outline-secondary d-block mb-2" href="{% url 'account_update' user.username %}">Update user details</a>
                        {% endif %}
                        <a class="btn btn-primary d-block mb-2" href="{% url 'account_profile_update' user.username %}" id="update-profile-link">Edit profile</a>

                        {% if request.user.is_superuser or is_cid_user_manager %}
                            <form method="post" action="{% url 'account_send_password_reset' user.username %}" class="mb-2">
                                {% csrf_token %}
                                <input type="hidden" name="next" value="{{ request.get_full_path }}">
                                <button type="submit" class="btn btn-outline-warning d-block w-100">Send password reset link</button>
                            </form>

                            {% if not supervisor_account %}
                                <form method="post" action="{% url 'account_set_supervisor' user.username %}" class="mb-2">
                                    {% csrf_token %}
                                    <input type="hidden" name="next" value="{{ request.get_full_path }}">
                                    <button type="submit" class="btn btn-outline-success d-block w-100">Set up as supervisor</button>
                                </form>
                            {% else %}
                                <a class="btn btn-outline-success d-block mb-2" href="{% url 'generic:supervisor_detail' supervisor_account.pk %}">Open supervisor record</a>
                            {% endif %}
                        {% endif %}

                        {% if request.user == user %}
                            <a class="btn btn-outline-secondary d-block mb-2" href="{% url 'atlas:api_tokens' %}">API tokens</a>
                        {% endif %}
                        {% if request.user.is_superuser %}
                            <a class="btn btn-outline-danger d-block" href="{% url 'admin:auth_user_change' user.pk %}" target="_blank" rel="noopener">Admin edit</a>
                        {% endif %}
                    </div>
                </div>
            </div>

            <div class="col-md-8">
                <div class="card mb-3">
                    <div class="card-header">Groups</div>
                    <div class="card-body">
                        {% include 'generic/partials/user_groups_fragment.html' with user=user %}
                    </div>
                </div>

                <div class="card">
                    <div class="card-header">Exams</div>
                    <div class="card-body">
                        {% with user.userprofile.get_exams as exam_map %}
                            {% if exam_map %}
                                {% for exam_type, exams in exam_map.items %}
                                    <h5 class="mt-2">{{ exam_type }}</h5>
                                    <ul class="list-group mb-2">
                                        {% for exam in exams %}
                                            <li class="list-group-item py-2">
                                                <a href="{{ exam.get_absolute_url }}">{{ exam }}</a>
                                            </li>
                                        {% endfor %}
                                    </ul>
                                {% endfor %}
                            {% else %}
                                <div class="text-muted">No exams.</div>
                            {% endif %}
                        {% endwith %}
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endblock %}