// src/renderer/src/utils/logger.ts

const log = (level: 'INFO' | 'WARN' | 'ERROR', message: string, data?: any) => {
  const timestamp = new Date().toISOString();
  let logMessage = `[${timestamp}] [${level}] ${message}`;
  
  if (data) {
    try {
      const dataStr = typeof data === 'object' ? JSON.stringify(data) : String(data);
      logMessage += ` | Data: ${dataStr}`;
    } catch (e) {
      logMessage += ` | Data: [Circular or Unserializable]`;
    }
  }

  // Console output for devtools
  const style = level === 'ERROR' ? 'color: red' : level === 'WARN' ? 'color: orange' : 'color: blue';
  console.log(`%c${logMessage}`, style);

  // Send to main process for file logging
  if (window.electron && window.electron.ipcRenderer) {
    window.electron.ipcRenderer.send('log-message', logMessage);
  }
};

export const logger = {
  info: (message: string, data?: any) => log('INFO', message, data),
  warn: (message: string, data?: any) => log('WARN', message, data),
  error: (message: string, data?: any) => log('ERROR', message, data),
};
