let icon = document.getElementById("chatIcon");
let chat = document.getElementById("chatWindow");

icon.onclick = function () {
    chat.style.display = "flex";
};

function toggleChat() {
    chat.style.display = "none";
}

async function sendMessage() {

    let input = document.getElementById("question");
    let question = input.value.trim();
    let chatBox = document.getElementById("chatBox");

    if (question === "") return;

    chatBox.innerHTML += `<div class="user">${question}</div>`;
    input.value = "";

    chatBox.scrollTop = chatBox.scrollHeight;

    let response = await fetch("/api/chat", {
        method: "POST",
        headers: {
            "Content-Type": "application/json"
        },
        body: JSON.stringify({ question: question })
    });

    let data = await response.text();

    chatBox.innerHTML += `<div class="ai">${data}</div>`;

    chatBox.scrollTop = chatBox.scrollHeight;
}