import { WebServer } from "@/web/server";

/**
 * Centralized logging utility with consistent formatting
 */

const LOG_BUFFER_SIZE = 50;
const logBuffer: Array<{ time: string; type: string; message: string }> = [];

function addToBuffer(type: string, message: string) {
    const time = new Date().toLocaleTimeString();
    logBuffer.unshift({ time, type, message });
    if (logBuffer.length > LOG_BUFFER_SIZE) {
        logBuffer.pop();
    }
}

export function getRecentLogs() {
    return logBuffer;
}

export const logger = {
    /**
     * General information message
     */
    info: (message: string, ...args: any[]) => {
        console.log(`ℹ️  ${message}`, ...args);
        addToBuffer("info", message);
        try { WebServer.broadcastLog("info", message); } catch { }
    },

    /**
     * Success message
     */
    success: (message: string, ...args: any[]) => {
        console.log(`✅ ${message}`, ...args);
        addToBuffer("success", message);
        try { WebServer.broadcastLog("success", message); } catch { }
    },

    /**
     * Warning message
     */
    warn: (message: string, ...args: any[]) => {
        console.warn(`⚠️  ${message}`, ...args);
        addToBuffer("warning", message);
        try { WebServer.broadcastLog("warning", message); } catch { }
    },

    /**
     * Error message
     */
    error: (message: string, ...args: any[]) => {
        console.error(`❌ ${message}`, ...args);
        addToBuffer("error", message);
        try { WebServer.broadcastLog("error", message); } catch { }
    },

    /**
     * Debug message
     */
    debug: (message: string, ...args: any[]) => {
        console.log(`🔍 ${message}`, ...args);
        addToBuffer("debug", message);
        try { WebServer.broadcastLog("debug", message); } catch { }
    },
};
