const apiUrl = "http://localhost/kvalifikacija_html/first_page/php";

async function getChatHistory() {
    try {
        const response = await axios.get(`${apiUrl}/getChatHistory.php`);
        return response.data;
    } catch (error) {
        console.error("Error loading chat history:", error);
        return [];
    }
}
async function saveMessage(role, content) {
    console.log("Saving message:", { role, content }); // Log the data being sent
    try {
        const response = await axios.post(`${apiUrl}/saveMessage.php`, {
            content: content,
            sender: role
        });
        console.log("Response:", response.data); // Log the response from the server
        if (!response.data.success) {
            console.error("Error saving message:", response.data.error);
        }
    } catch (error) {
        console.error("Error saving message:", error);
    }
}


async function getChatHistory() {
    try {
        const response = await axios.get(`${apiUrl}/getChatHistory.php`);
        console.log("Chat history:", response.data); // Debug response
        return response.data;
    } catch (error) {
        console.error("Error loading chat history:", error);
        return [];
    }
}

// Example usage
document.addEventListener("DOMContentLoaded", async () => {
    const chatHistory = await getChatHistory();
    console.log("Chat History:", chatHistory);
});
