document.addEventListener('DOMContentLoaded', () => {
    const btnSuspend = document.getElementById('btn-suspend');
    const btnEnable = document.getElementById('btn-enable');
    const terminal = document.getElementById('terminal-logs');

    const appendLog = (message, colorClass) => {
        const now = new Date().toLocaleTimeString('fr-FR');
        const p = document.createElement('p');
        p.className = `${colorClass} font-bold mt-2`;
        p.innerHTML = `[${now}] ${message}`;
        terminal.appendChild(p);
        terminal.scrollTop = terminal.scrollHeight;
    };

    const sendAction = (disableValue, logMessage, color) => {
        const urlParams = new URLSearchParams(window.location.search);
        const secret = urlParams.get('secret');

        fetch(`${window.location.pathname}?secret=${secret}&disable=${disableValue}`, {
            headers: { 'X-Requested-With': 'XMLHttpRequest' }
        })
            .then(response => response.json())
            .then(data => {
                // Ligne d'action principale
                appendLog(`>> EXEC: ${logMessage}`, color);

                // Ligne de confirmation d'alerte
                setTimeout(() => {
                    appendLog(`[SYSTEM] SECURITY ALERT SENT TO ADMIN... OK`, 'text-slate-500 italic text-[10px]');
                }, 400);
            })
            .catch(error => {
                appendLog(`>> ERROR: SECURITY BREACH OR DISCONNECT`, 'text-orange-500');
            });
    };

    // Bouton Suspendre (disable=1 -> INTRANET_LOCK=true)
    btnSuspend.addEventListener('click', () => {
        sendAction('1', 'DISABLE ACCESS INTRANET (LOCK: TRUE)', 'text-red-500');
    });

    // Bouton Réactiver (disable=0 -> INTRANET_LOCK=false)
    btnEnable.addEventListener('click', () => {
        sendAction('0', 'RESTORE ACCESS INTRANET (LOCK: FALSE)', 'text-blue-400');
    });

    // ... Dans votre script existant ...

    const btnCache = document.getElementById('btn-cache');
    const btnLiip = document.getElementById('btn-liip');

    const executeSystemCommand = (action, logName, color) => {
        const urlParams = new URLSearchParams(window.location.search);
        const secret = urlParams.get('secret');

        appendLog(`>> STARTING: ${logName}...`, 'text-slate-400 italic');

        fetch(`${window.location.pathname}?secret=${secret}&action=${action}`, {
            headers: { 'X-Requested-With': 'XMLHttpRequest' }
        })
            .then(response => response.json())
            .then(data => {
                appendLog(`>> SUCCESS: ${data.message}`, color);
                appendLog(`[SYSTEM] NOTIFICATION SENT`, 'text-slate-500 text-[9px]');
            })
            .catch(() => {
                appendLog(`>> ERROR: EXECUTION FAILED`, 'text-red-500');
            });
    };

    btnCache.addEventListener('click', () => {
        executeSystemCommand('cache_clear', 'php bin/console cache:clear', 'text-blue-400');
    });

    btnLiip.addEventListener('click', () => {
        executeSystemCommand('liip_clear', 'liip:imagine:cache:remove', 'text-purple-400');
    });
});
