document.addEventListener("DOMContentLoaded", function () {
    const input = document.getElementById("command-input");
    const terminal = document.getElementById("terminal");
    const availableThemes = ["pwny", "light", "catppuccin", "doom1", "dracula", "gruvbox", "nord"];

    // Load the saved theme from localStorage, defaulting to "pwny"
    let currentTheme = localStorage.getItem("theme") || "pwny";
    applyTheme(currentTheme);

    input.addEventListener("keydown", function (event) {
        if (event.key === "Enter") {
            const command = input.value.trim();
            if (command.startsWith("theme")) {
                event.preventDefault();
                const parts = command.split(" ");
                if (parts.length === 1) {
                    addLogEntry(`Available themes: ${availableThemes.join(', ')}. Current theme: ${currentTheme}`, 'info');
                } else {
                    const theme = parts[1];
                    applyTheme(theme);
                    addLogEntry(`Theme changed to: ${theme}`, 'info');
                }
                input.value = ''; // Clear input
            }
        }
    });

    function applyTheme(themeName) {
        if (availableThemes.includes(themeName)) {
            document.body.className = "";  // Clear all theme classes
            document.body.classList.add(`${themeName}-theme`);  // Add the new theme class
            localStorage.setItem("theme", themeName);
            currentTheme = themeName;
        } else {
            addLogEntry(`Error: Theme "${themeName}" not found. Available themes: ${availableThemes.join(', ')}`, 'error');
        }
    }

    function addLogEntry(message, type) {
        const logEntry = document.createElement("div");
        logEntry.classList.add(type === 'error' ? 'error' : 'info');
        logEntry.textContent = message;
        terminal.appendChild(logEntry);
        terminal.scrollTop = terminal.scrollHeight;
    }
});