pragma ComponentBehavior: Bound

import ZShell.Config
import ZShell.Llm
import QtQuick
import QtQuick.Controls
import Quickshell
import qs.Helpers
import qs.Modules.Notifications.Sidebar
import qs.Components
import qs.Services

Item {
	id: root

	anchors.fill: parent
	anchors.margins: Tokens.padding.small

	Component.onCompleted: {
		if (ChatState.inChat) {
			stack.push(chatList);
			stack.push(chatContent, {
				"chatData": ChatState.chatSession
			});
		} else
			stack.push(chatList);
	}

	StackView {
		id: stack
		anchors.fill: parent
	}

	Component {
		id: chatList

		ChatList {
			model: ScriptModel {
				values: Chat.chats.values
			}

			onDeleteChatRequest: chat => {
				Chat.chats.remove(chat);
			}
			onLoadChatRequest: (chat, index) => {
				stack.push(chatContent, {
					"chatData": chat
				});
				ChatState.inChat = true;
				ChatState.chatSession = chat;
			}
			onNewChatRequest: {
				const data = Chat.chats.insert();
				stack.push(chatContent, {
					"chatData": data
				});
				ChatState.inChat = true;
				ChatState.chatSession = data;
			}
		}
	}

	Component {
		id: chatContent

		ChatContent {
			onRequestClose: {
				stack.pop();
				ChatState.inChat = false;
			}
		}
	}
}
