{% set current_theme = current_theme() %}
<!doctype html>
<html lang="{{ current_locale() }}" class="h-100">
    <head>
        {{ current_theme.headertags() }}
        <title>{{ _("Mensajes") }} - {{ course.nombre }}</title>
        {{ current_theme.local_style() }}
    </head>

    <body>
        {{ current_theme.navbar() }}

        <main>
            {{ current_theme.notify() }}

            <div class="container px-4 py-5">
                <div class="d-flex justify-content-between align-items-center pb-2 border-bottom">
                    <h4>
                        <nav aria-label="breadcrumb">
                            <ol class="breadcrumb">
                                <li class="breadcrumb-item">
                                    <a class="text-reset text-decoration-none link-dark" href="{{ url_for('home.panel') }}">
                                        {{ _("Inicio") }}
                                    </a>
                                </li>
                                <li class="breadcrumb-item">
                                    <a
                                        class="text-reset text-decoration-none link-dark"
                                        href="{{ url_for('course.curso', course_code=course.codigo) }}"
                                    >
                                        {{ course.nombre }}
                                    </a>
                                </li>
                                <li class="breadcrumb-item active">{{ _("Mensajes") }}</li>
                            </ol>
                        </nav>
                    </h4>
                    {% if current_user.tipo == "student" %}
                    <a href="{{ url_for('msg.new_thread', course_code=course.codigo) }}" class="btn btn-primary">
                        <i class="bi bi-plus-circle"></i> {{ _("Nuevo Mensaje") }}
                    </a>
                    {% endif %}
                </div>

                {% if threads %}
                <div class="row">
                    <div class="col-12">
                        {% for thread in threads %}
                        <div class="card mb-3">
                            <div class="card-body">
                                <div class="d-flex justify-content-between align-items-start">
                                    <div class="flex-grow-1">
                                        <h6 class="card-title">
                                            <a
                                                href="{{ url_for('msg.view_thread', thread_id=thread.id) }}"
                                                class="text-decoration-none"
                                            >
                                                {% set first_message = thread.messages.first() %} {% if first_message %} {{
                                                first_message.content.split('\n')[0][:100] }}... {% else %} Thread #{{
                                                thread.id[:8] }} {% endif %}
                                            </a>
                                        </h6>
                                        <p class="card-text">
                                            <small class="text-muted">
                                                Por: {{ thread.student.nombre }} {{ thread.student.apellido }} | {{
                                                thread.timestamp.strftime('%d/%m/%Y %H:%M') }} | {{ _("Mensajes") }}: {{
                                                thread.messages.count() }}
                                            </small>
                                        </p>
                                    </div>
                                    <div class="ms-3">
                                        {% if thread.status == "open" %}
                                        <span class="badge bg-success">{{ _("Abierto") }}</span>
                                        {% elif thread.status == "fixed" %}
                                        <span class="badge bg-warning">Resuelto</span>
                                        {% elif thread.status == "closed" %}
                                        <span class="badge bg-secondary">{{ _("Cerrado") }}</span>
                                        {% endif %}
                                    </div>
                                </div>
                            </div>
                        </div>
                        {% endfor %}
                    </div>
                </div>
                {% else %}
                <div class="text-center py-5">
                    <i class="bi bi-chat-left-text" style="font-size: 3rem; color: #6c757d"></i>
                    <h5 class="mt-3 text-muted">No hay mensajes</h5>
                    {% if current_user.tipo == "student" %}
                    <p class="text-muted">¿Tienes alguna pregunta sobre el curso?</p>
                    <a href="{{ url_for('msg.new_thread', course_code=course.codigo) }}" class="btn btn-primary">
                        <i class="bi bi-plus-circle"></i> {{ _("Enviar") }} Primer {{ _("Mensaje") }}
                    </a>
                    {% else %}
                    <p class="text-muted">No hay mensajes de estudiantes en este curso.</p>
                    {% endif %}
                </div>
                {% endif %}
            </div>
        </main>
    </body>
</html>
