{% extends "layouts/base.html" %}
{% load i18n %}

{% block title %}{{ document.title }} - {% trans "Reference Section" %}{% endblock %}

{% block extra_css %}
<style>
    /* PX360 App Theme Variables */
    :root {
        --hh-navy: #005696;
        --hh-blue: #007bbd;
        --hh-light: #eef6fb;
        --hh-slate: #64748b;
    }

    /* Page Header */
    .page-header-gradient {
        background: linear-gradient(135deg, var(--hh-navy) 0%, #0069a8 50%, var(--hh-blue) 100%);
        color: white;
        padding: 1.5rem 2rem;
        border-radius: 1rem;
        margin-bottom: 1.5rem;
        box-shadow: 0 10px 15px -3px rgba(0, 86, 150, 0.2);
    }

    /* Section Cards */
    .section-card {
        background: white;
        border-radius: 1rem;
        border: 2px solid #e2e8f0;
        box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
        overflow: hidden;
        transition: all 0.3s ease;
    }
    .section-card:hover {
        border-color: #005696;
        box-shadow: 0 10px 25px -5px rgba(0, 86, 150, 0.15);
    }

    .section-header {
        padding: 1rem 1.5rem;
        border-bottom: 2px solid #e2e8f0;
        background: linear-gradient(to right, #f8fafc, #f1f5f9);
        display: flex;
        align-items: center;
        gap: 0.75rem;
    }

    .section-icon {
        width: 40px;
        height: 40px;
        border-radius: 0.75rem;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    /* Detail Rows */
    .detail-row {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 1rem 1.5rem;
        border-bottom: 1px solid #f1f5f9;
    }
    .detail-row:hover {
        background-color: #f8fafc;
    }
    .detail-row:last-child {
        border-bottom: none;
    }

    /* Table Styling */
    .data-table {
        width: 100%;
        border-collapse: collapse;
    }
    .data-table th {
        background: #f8fafc;
        border-bottom: 2px solid #e2e8f0;
        padding: 0.75rem 1rem;
        text-align: left;
        font-size: 0.75rem;
        font-weight: 600;
        color: #64748b;
        text-transform: uppercase;
        letter-spacing: 0.025em;
    }
    .data-table td {
        padding: 1rem;
        border-bottom: 1px solid #f1f5f9;
        vertical-align: middle;
    }
    .data-table tr:hover td {
        background: #f8fafc;
    }

    /* Button Transitions */
    .btn-transition {
        transition: all 0.2s ease-in-out;
    }

    /* Badge */
    .badge-px360 {
        display: inline-flex;
        align-items: center;
        padding: 0.25rem 0.75rem;
        border-radius: 9999px;
        font-size: 0.75rem;
        font-weight: 500;
    }
    .badge-green {
        background: #dcfce7;
        color: #166534;
    }
    .badge-slate {
        background: #f1f5f9;
        color: #64748b;
    }
    .badge-blue {
        background: #dbeafe;
        color: #1e40af;
    }

    /* Action Buttons */
    .action-btn {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        padding: 0.75rem 1rem;
        border-radius: 0.5rem;
        font-weight: 500;
        transition: all 0.2s;
        width: 100%;
    }
    .action-btn-primary {
        background: #005696;
        color: white;
    }
    .action-btn-primary:hover {
        background: #007bbd;
    }
    .action-btn-secondary {
        background: white;
        border: 2px solid #e2e8f0;
        color: #374151;
    }
    .action-btn-secondary:hover {
        background: #f8fafc;
        border-color: #005696;
    }
    .action-btn-success {
        background: white;
        border: 2px solid #22c55e;
        color: #22c55e;
    }
    .action-btn-success:hover {
        background: #22c55e;
        color: white;
    }
    .action-btn-danger {
        background: white;
        border: 2px solid #ef4444;
        color: #ef4444;
    }
    .action-btn-danger:hover {
        background: #ef4444;
        color: white;
    }

    /* Related Document Link */
    .related-doc-link {
        display: flex;
        align-items: center;
        padding: 0.75rem 1rem;
        transition: all 0.2s;
        text-decoration: none;
        color: inherit;
    }
    .related-doc-link:hover {
        background: #f8fafc;
    }
</style>
{% endblock %}

{% block content %}
<div class="px-6 py-4">
    <!-- Page Header -->
    <div class="page-header-gradient">
        <div class="flex justify-between items-start">
            <div>
                <nav aria-label="breadcrumb" class="mb-1">
                    <ol class="flex items-center space-x-2 text-sm text-blue-100 flex-wrap">
                        <li>
                            <a href="{% url 'references:dashboard' %}" class="hover:text-white transition">{% trans "Reference Section" %}</a>
                        </li>
                        {% if document.folder %}
                            {% for folder in document.folder.get_full_path_folders %}
                                <li><span class="mx-2">/</span></li>
                                <li><a href="{% url 'references:folder_view' folder.id %}" class="hover:text-white transition">{{ folder.name }}</a></li>
                            {% endfor %}
                        {% endif %}
                        <li><span class="mx-2">/</span></li>
                        <li class="text-white font-medium">{{ document.title }}</li>
                    </ol>
                </nav>
                <h1 class="text-2xl font-bold flex items-center gap-2">
                    <i class="{{ document.get_file_icon }}"></i>
                    {{ document.title }}
                </h1>
                {% if document.description %}
                    <p class="text-blue-100 text-sm mt-1">{{ document.description }}</p>
                {% endif %}
            </div>
            <div class="flex gap-2">
                <a href="{% url 'references:document_edit' document.id %}" class="btn-transition inline-flex items-center px-4 py-2.5 bg-white/10 border-2 border-white/30 text-white font-medium rounded-xl hover:bg-white hover:text-navy transition">
                    <i data-lucide="edit" class="w-4 h-4 me-2"></i>{% trans "Edit" %}
                </a>
                <a href="{% url 'references:api_document_download' document.id %}" class="btn-transition inline-flex items-center px-4 py-2.5 bg-white text-navy font-medium rounded-xl hover:bg-blue-50 transition">
                    <i data-lucide="download" class="w-4 h-4 me-2"></i>{% trans "Download" %}
                </a>
            </div>
        </div>
    </div>

    <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
        <!-- Document Details -->
        <div class="lg:col-span-2">
            <div class="section-card mb-6">
                <div class="section-header">
                    <div class="section-icon bg-navy/10">
                        <i data-lucide="file-text" class="w-5 h-5 text-navy"></i>
                    </div>
                    <h5 class="text-lg font-semibold text-gray-800">{% trans "Document Details" %}</h5>
                </div>
                <div class="p-0">
                    <div class="detail-row">
                        <span class="text-sm text-slate-500">{% trans "File Name" %}</span>
                        <span class="text-sm font-semibold text-gray-800">{{ document.filename }}</span>
                    </div>
                    <div class="detail-row">
                        <span class="text-sm text-slate-500">{% trans "File Type" %}</span>
                        <span class="badge-px360 badge-slate">{{ document.file_type|upper }}</span>
                    </div>
                    <div class="detail-row">
                        <span class="text-sm text-slate-500">{% trans "File Size" %}</span>
                        <span class="text-sm font-semibold text-gray-800">{{ document.get_file_size_display }}</span>
                    </div>
                    <div class="detail-row">
                        <span class="text-sm text-slate-500">{% trans "Version" %}</span>
                        {% if document.is_latest_version %}
                            <span class="badge-px360 badge-green">{{ document.version }}</span>
                        {% else %}
                            <span class="badge-px360 badge-slate">{{ document.version }}</span>
                        {% endif %}
                    </div>
                    <div class="detail-row">
                        <span class="text-sm text-slate-500">{% trans "Downloads" %}</span>
                        <span class="text-sm font-semibold text-gray-800">{{ document.download_count }}</span>
                    </div>
                    <div class="detail-row">
                        <span class="text-sm text-slate-500">{% trans "Uploaded By" %}</span>
                        <span class="text-sm font-semibold text-gray-800">
                            {% if document.uploaded_by %}{{ document.uploaded_by.get_full_name }}{% else %}{% trans "Unknown" %}{% endif %}
                        </span>
                    </div>
                    <div class="detail-row">
                        <span class="text-sm text-slate-500">{% trans "Created" %}</span>
                        <span class="text-sm font-semibold text-gray-800">{{ document.created_at|date:"M d, Y H:i" }}</span>
                    </div>
                    <div class="detail-row">
                        <span class="text-sm text-slate-500">{% trans "Last Modified" %}</span>
                        <span class="text-sm font-semibold text-gray-800">{{ document.updated_at|date:"M d, Y H:i" }}</span>
                    </div>
                    {% if document.tags %}
                        <div class="detail-row">
                            <span class="text-sm text-slate-500">{% trans "Tags" %}</span>
                            <div class="flex gap-1">
                                {% for tag in document.tags_list %}
                                    <span class="badge-px360 badge-blue">{{ tag }}</span>
                                {% endfor %}
                            </div>
                        </div>
                    {% endif %}
                </div>
            </div>

            <!-- Version History -->
            {% if versions %}
                <div class="section-card">
                    <div class="section-header">
                        <div class="section-icon bg-blue/10">
                            <i data-lucide="history" class="w-5 h-5 text-blue"></i>
                        </div>
                        <h5 class="text-lg font-semibold text-gray-800">{% trans "Version History" %}</h5>
                    </div>
                    <div class="p-0">
                        <table class="data-table">
                            <thead>
                                <tr>
                                    <th>{% trans "Version" %}</th>
                                    <th>{% trans "File" %}</th>
                                    <th>{% trans "Size" %}</th>
                                    <th>{% trans "Created" %}</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for version in versions %}
                                    <tr{% if version.is_latest_version %} class="bg-blue-50/50"{% endif %}>
                                        <td>
                                            {% if version.is_latest_version %}
                                                <span class="badge-px360 badge-green">{{ version.version }}</span>
                                            {% else %}
                                                <span class="badge-px360 badge-slate">{{ version.version }}</span>
                                            {% endif %}
                                        </td>
                                        <td class="text-sm text-gray-700">{{ version.filename }}</td>
                                        <td class="text-sm text-slate-600">{{ version.get_file_size_display }}</td>
                                        <td class="text-sm text-slate-600">{{ version.created_at|date:"M d, Y H:i" }}</td>
                                    </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </div>
            {% endif %}
        </div>

        <!-- Sidebar -->
        <div class="lg:col-span-1">
            <!-- Actions -->
            <div class="section-card mb-6">
                <div class="section-header">
                    <div class="section-icon bg-green-500/10">
                        <i data-lucide="wrench" class="w-5 h-5 text-green-600"></i>
                    </div>
                    <h5 class="text-lg font-semibold text-gray-800">{% trans "Actions" %}</h5>
                </div>
                <div class="p-4 space-y-2">
                    <a href="{% url 'references:api_document_download' document.id %}" class="action-btn action-btn-primary">
                        <i data-lucide="download" class="w-4 h-4 me-2"></i>{% trans "Download Document" %}
                    </a>
                    <a href="{% url 'references:document_edit' document.id %}" class="action-btn action-btn-secondary">
                        <i data-lucide="edit" class="w-4 h-4 me-2"></i>{% trans "Edit Metadata" %}
                    </a>
                    {% if document.folder %}
                        <a href="{% url 'references:document_create_in_folder' folder_pk=document.folder.id %}" class="action-btn action-btn-success">
                            <i data-lucide="upload" class="w-4 h-4 me-2"></i>{% trans "Upload New Version" %}
                        </a>
                    {% endif %}
                    <button type="button" onclick="confirmDelete()" class="action-btn action-btn-danger">
                        <i data-lucide="trash-2" class="w-4 h-4 me-2"></i>{% trans "Delete Document" %}
                    </button>
                </div>
            </div>

            <!-- Description -->
            {% if document.description %}
                <div class="section-card mb-6">
                    <div class="section-header">
                        <div class="section-icon bg-slate/10">
                            <i data-lucide="align-left" class="w-5 h-5 text-slate"></i>
                        </div>
                        <h5 class="text-lg font-semibold text-gray-800">{% trans "Description" %}</h5>
                    </div>
                    <div class="p-4">
                        <p class="text-gray-700 text-sm">{{ document.description|linebreaks }}</p>
                    </div>
                </div>
            {% endif %}

            <!-- Related Documents -->
            {% if related_documents %}
                <div class="section-card">
                    <div class="section-header">
                        <div class="section-icon bg-blue/10">
                            <i data-lucide="link" class="w-5 h-5 text-blue"></i>
                        </div>
                        <h5 class="text-lg font-semibold text-gray-800">{% trans "Related Documents" %}</h5>
                    </div>
                    <div class="p-0">
                        {% for related in related_documents %}
                            <a href="{% url 'references:document_view' related.id %}" class="related-doc-link border-b border-gray-100 last:border-0">
                                <i class="{{ related.get_file_icon }} w-5 h-5 text-blue me-2"></i>
                                <span class="text-sm font-medium text-gray-800 truncate">{{ related.title|truncatechars:30 }}</span>
                            </a>
                        {% endfor %}
                    </div>
                </div>
            {% endif %}
        </div>
    </div>
</div>

<form id="deleteForm" method="post" action="{% url 'references:document_delete' document.id %}" style="display: none;">
    {% csrf_token %}
</form>

<script>
function confirmDelete() {
    if (confirm('{% trans "Are you sure you want to delete this document?" %}')) {
        document.getElementById('deleteForm').submit();
    }
}
</script>

            <!-- Version History -->
            {% if versions %}
                <div class="section-card">
                    <div class="section-header">
                        <div class="section-icon bg-blue bg-opacity-10">
                            <i data-lucide="history" class="w-6 h-6 text-blue"></i>
                        </div>
                        <h5 class="text-lg font-semibold text-gray-800">{% trans "Version History" %}</h5>
                    </div>
                    <div class="p-0">
                        <div class="overflow-x-auto">
                            <table class="w-full">
                                <thead>
                                    <tr class="bg-slate-50 border-b-2 border-gray-200">
                                        <th class="text-left text-xs font-semibold text-slate-600 uppercase tracking-wider px-6 py-3">{% trans "Version" %}</th>
                                        <th class="text-left text-xs font-semibold text-slate-600 uppercase tracking-wider px-6 py-3">{% trans "File" %}</th>
                                        <th class="text-left text-xs font-semibold text-slate-600 uppercase tracking-wider px-6 py-3">{% trans "Size" %}</th>
                                        <th class="text-left text-xs font-semibold text-slate-600 uppercase tracking-wider px-6 py-3">{% trans "Created" %}</th>
                                    </tr>
                                </thead>
                                <tbody class="divide-y divide-gray-100">
                                    {% for version in versions %}
                                        <tr{% if version.is_latest_version %} class="bg-blue bg-opacity-5"{% endif %}>
                                            <td class="px-6 py-4">
                                                {% if version.is_latest_version %}
                                                    <span class="px-2.5 py-1 bg-green-100 text-green-700 text-xs font-medium rounded-full">{{ version.version }}</span>
                                                {% else %}
                                                    <span class="px-2.5 py-1 bg-gray-100 text-gray-700 text-xs font-medium rounded-full">{{ version.version }}</span>
                                                {% endif %}
                                            </td>
                                            <td class="px-6 py-4 text-sm text-gray-700">{{ version.filename }}</td>
                                            <td class="px-6 py-4 text-sm text-slate-600">{{ version.get_file_size_display }}</td>
                                            <td class="px-6 py-4 text-sm text-slate-600">{{ version.created_at|date:"M d, Y H:i" }}</td>
                                        </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            {% endif %}
        </div>

        <!-- Sidebar -->
        <div class="lg:col-span-1">
            <!-- Actions -->
            <div class="section-card mb-6">
                <div class="section-header">
                    <div class="section-icon bg-green-600 bg-opacity-10">
                        <i data-lucide="wrench" class="w-6 h-6 text-green-600"></i>
                    </div>
                    <h5 class="text-lg font-semibold text-gray-800">{% trans "Actions" %}</h5>
                </div>
                <div class="p-6">
                    <div class="flex flex-col gap-2">
                        <a href="{% url 'references:api_document_download' document.id %}" class="btn-transition inline-flex justify-center items-center px-4 py-2.5 bg-navy text-white font-medium rounded-xl hover:bg-blue transition">
                            <i data-lucide="download" class="w-4 h-4 me-2"></i>{% trans "Download Document" %}
                        </a>
                        <a href="{% url 'references:document_edit' document.id %}" class="btn-transition inline-flex justify-center items-center px-4 py-2.5 bg-white border-2 border-navy text-navy font-medium rounded-xl hover:bg-navy hover:text-white transition">
                            <i data-lucide="edit" class="w-4 h-4 me-2"></i>{% trans "Edit Metadata" %}
                        </a>
                        {% if document.folder %}
                        <a href="{% url 'references:document_create_in_folder' folder_pk=document.folder.id %}" class="btn-transition inline-flex justify-center items-center px-4 py-2.5 bg-white border-2 border-green-600 text-green-600 font-medium rounded-xl hover:bg-green-600 hover:text-white transition">
                            <i data-lucide="upload" class="w-4 h-4 me-2"></i>{% trans "Upload New Version" %}
                        </a>
                        {% endif %}
                        <button type="button" onclick="confirmDelete()" class="btn-transition inline-flex justify-center items-center px-4 py-2.5 bg-white border-2 border-red-500 text-red-500 font-medium rounded-xl hover:bg-red-500 hover:text-white transition">
                            <i data-lucide="trash-2" class="w-4 h-4 me-2"></i>{% trans "Delete Document" %}
                        </button>
                    </div>
                </div>
            </div>

            <!-- Description -->
            {% if document.description %}
                <div class="section-card mb-6">
                    <div class="section-header">
                        <div class="section-icon bg-slate bg-opacity-10">
                            <i data-lucide="align-left" class="w-6 h-6 text-slate"></i>
                        </div>
                        <h5 class="text-lg font-semibold text-gray-800">{% trans "Description" %}</h5>
                    </div>
                    <div class="p-6">
                        <p class="text-gray-700 text-sm mb-0">{{ document.description|linebreaks }}</p>
                    </div>
                </div>
            {% endif %}

            <!-- Related Documents -->
            {% if related_documents %}
                <div class="section-card">
                    <div class="section-header">
                        <div class="section-icon bg-blue bg-opacity-10">
                            <i data-lucide="link" class="w-6 h-6 text-blue"></i>
                        </div>
                        <h5 class="text-lg font-semibold text-gray-800">{% trans "Related Documents" %}</h5>
                    </div>
                    <div class="p-0">
                        <div class="divide-y divide-gray-100">
                            {% for related in related_documents %}
                                <a href="{% url 'references:document_view' related.id %}" class="flex items-center p-4 hover:bg-slate-50 transition block">
                                    <i class="{{ related.get_file_icon }} w-8 h-8 text-blue"></i>
                                    <span class="ml-3 text-sm font-medium text-gray-800">{{ related.title|truncatechars:30 }}</span>
                                </a>
                            {% endfor %}
                        </div>
                    </div>
                </div>
            {% endif %}
        </div>
    </div>
</div>

{% endblock %}
