{% extends 'django_spire/page/full_page.html' %}

{% block full_page_content_class %}{% endblock %}

{% block full_page_content %}
    <div
        class="d-flex flex-column overflow-hidden vh-minus-top-nav"
        x-data="{
            chat_load_ajax_view: new ViewGlue('{% url "django_spire:ai:chat:template:load" %}'),
            is_narrow_viewport: window.innerWidth < 768,
            show_history: false,

            init() {
                this.load_chat(0)
                this.show_history = !this.is_narrow_viewport

                window.addEventListener('resize', () => {
                    this.is_narrow_viewport = window.innerWidth < 768
                })
            },

            async load_chat(chat_id) {
                await this.chat_load_ajax_view.render_inner(this.$refs.chat_dialog, {
                        chat_id: chat_id
                    }
                )

                $dispatch('chat-loaded')
            }
        }"
        @deleted_chat.window="load_chat(0)"
        @load-chat.window="load_chat($event.detail.chat_id); if (is_narrow_viewport) show_history = false"
    >
        <div class="d-flex align-items-center px-4 border-bottom border-app-primary flex-shrink-0 bg-app-layer-one" style="height: 52px;">
            <div class="d-flex align-items-center gap-2">
                <a
                    @click="show_history = !show_history"
                    class="d-flex align-items-center gap-2 rounded px-2 py-1 cursor-pointer ds-fs-10"
                    role="button"
                    style="transition: background-color 0.15s ease;"
                    :class="show_history ? 'bg-app-primary-soft text-app-primary' : 'text-muted'"
                >
                    <i class="bi bi-clock-history ds-fs-7"></i>
                    <span class="fw-medium">Chat History</span>
                </a>
            </div>
        </div>

        <div class="d-flex flex-grow-1 overflow-hidden">
            <div
                class="flex-shrink-0 bg-app-layer-one overflow-hidden"
                :class="is_narrow_viewport ? '' : ''"
                :style="show_history
                    ? (is_narrow_viewport
                        ? 'position: fixed; width: 100vw; height: 100vh; left: 0; top: 0; z-index: 1040;'
                        : 'width: 320px; border-right: 1px solid var(--app-primary);')
                    : (is_narrow_viewport
                        ? 'display: none;'
                        : 'width: 0;')"
                style="transition: width 0.3s ease;"
            >
                <div :style="is_narrow_viewport ? 'width: 100%; height: 100%;' : 'width: 320px; height: 100%;'">
                    {% include 'django_spire/ai/chat/widget/selection_widget.html' %}
                </div>
            </div>

            <div class="flex-grow-1 overflow-hidden" style="min-width: 0;">
                <div class="h-100" x-ref="chat_dialog"></div>
            </div>
        </div>
    </div>
{% endblock %}
