{% extends 'textdocs/base.html' %}
{% block content %}

    <a href="{% url 'document_list' %}" class="btn btn-secondary mb-3">‹ Back to Document List</a>

    <div class="card">
        <div class="card-header">
            <div class="d-flex justify-content-between align-items-center">
                <!-- Title and Byline Grouped Together -->
                <div>
                    <h2>{{ document.title }}</h2>
                    <!-- NEW: Byline with author's name -->
                    <h6 class="card-subtitle text-muted">
                        Latest version v{{ document.latest_version.version_number }}
                        By {{ document.author.nombre }} on {{ document.latest_version.created_at|date:"F d, Y" }}
                    </h6>
                </div>
                <!-- Action Buttons -->
                <div>
                    <a href="{% url 'document_edit' pk=document.pk %}" class="btn btn-secondary">Edit</a>
                    <a href="{% url 'document_delete' pk=document.pk %}" class="btn btn-danger">Delete</a>
                    <a href="{% url 'generate_pdf' pk=document.pk %}" class="btn btn-info">Export to PDF</a>
                </div>
            </div>
        </div>
        <div class="card-body">
            <h4 class="card-title">Introduction</h4>
            <div>{{ document.latest_version.introduction|safe }}</div>
            <hr>
            <h4 class="card-title">Main Body</h4>
            <div>{{ document.latest_version.main_body|safe }}</div>
            <hr>
            <h4 class="card-title">Conclusion</h4>
            <div>{{ document.latest_version.conclusion|safe }}</div>
        </div>
        <div class="mt-4">
        <h4>Version History</h4>
            <ul class="list-group">
                {% for version in document.versions.all %}
                    <li class="list-group-item">
                        <a href="{% url 'view_version' pk=document.pk version_number=version.version_number %}">
                            Version {{ version.version_number }}
                        </a>
                        <small class="text-muted"> - created on {{ version.created_at|date:"F d, Y" }}</small>
                        {% if version == document.latest_version %}<span class="badge bg-success ms-2">Latest</span>{% endif %}
                    </li>
                {% endfor %}
            </ul>
        </div>
    </div>
{% endblock %}