/*
 * SPDX-FileCopyrightText: 2026 Laurent Montel <montel@kde.org>
 *
 * SPDX-License-Identifier: LGPL-2.0-or-later
 */
#include "kaichatdatabaseselectchatwidget.h"
#include "databasedialog/kaichatdatabasemessageslistview.h"
#include "databasedialog/kaichatdatabaseselectchatlineedit.h"
#include <QVBoxLayout>
#include <TextAutoGenerateText/TextAutoGenerateLocalDatabaseManager>
#include <TextAutoGenerateText/TextAutoGenerateLocalMessagesDatabase>
#include <TextAutoGenerateText/TextAutoGenerateManager>

using namespace Qt::Literals::StringLiterals;
KAIChatDatabaseSelectChatWidget::KAIChatDatabaseSelectChatWidget(TextAutoGenerateText::TextAutoGenerateManager *manager, QWidget *parent)
    : QWidget{parent}
    , mMessagesListView(new KAIChatDatabaseMessagesListView(manager, this))
    , mLineEdit(new KAIChatDatabaseSelectChatLineEdit(manager, this))
    , mManager(manager)
{
    auto mainLayout = new QVBoxLayout(this);
    mainLayout->setObjectName(u"mainLayout"_s);
    mainLayout->setContentsMargins({});

    mLineEdit->setObjectName(u"mLineEdit"_s);
    mainLayout->addWidget(mLineEdit);

    mMessagesListView->setObjectName(u"mMessagesListView"_s);
    mainLayout->addWidget(mMessagesListView);

    connect(mLineEdit, &KAIChatDatabaseSelectChatLineEdit::selectChatId, this, &KAIChatDatabaseSelectChatWidget::selectChatId);
}

KAIChatDatabaseSelectChatWidget::~KAIChatDatabaseSelectChatWidget() = default;

void KAIChatDatabaseSelectChatWidget::loadMessages(const QString &chatId)
{
    if (mManager) {
        const auto messages = mManager->databaseManager()->messagesDatabase()->loadMessages(chatId);
        mMessagesListView->setMessages(messages);
    }
}

#include "moc_kaichatdatabaseselectchatwidget.cpp"
