/**
 * Structured Logging Utility
 * 
 * Provides structured logging with different log levels.
 * In production, logs can be sent to error tracking services.
 * 
 * Usage:
 *   logger.info('User logged in', { userId: '123' });
 *   logger.error('API request failed', { error, url });
 */

export enum LogLevel {
  DEBUG = 'debug',
  INFO = 'info',
  WARN = 'warn',
  ERROR = 'error',
}

interface LogContext {
  [key: string]: unknown;
}

class Logger {
  private isDevelopment = import.meta.env.DEV;
  private isProduction = import.meta.env.PROD;

  /**
   * Log debug messages (only in development)
   */
  debug(message: string, context?: LogContext): void {
    if (this.isDevelopment) {
      console.debug(`[DEBUG] ${message}`, context || '');
    }
  }

  /**
   * Log informational messages
   */
  info(message: string, context?: LogContext): void {
    if (this.isDevelopment) {
      console.info(`[INFO] ${message}`, context || '');
    }
    // In production, could send to analytics service
  }

  /**
   * Log warning messages
   */
  warn(message: string, context?: LogContext): void {
    console.warn(`[WARN] ${message}`, context || '');
    // In production, could send to monitoring service
  }

  /**
   * Log error messages
   */
  error(message: string, error?: Error | unknown, context?: LogContext): void {
    const errorContext = {
      ...context,
      error: error instanceof Error ? {
        message: error.message,
        stack: error.stack,
        name: error.name,
      } : error,
    };

    console.error(`[ERROR] ${message}`, errorContext);

    // In production, send to error tracking service (e.g., Sentry)
    if (this.isProduction && error) {
      // TODO: Integrate with error tracking service
      // Example: Sentry.captureException(error, { contexts: { custom: context } });
    }
  }

  /**
   * Log API requests (development only)
   */
  logRequest(method: string, url: string, data?: unknown): void {
    if (this.isDevelopment) {
      this.debug(`API ${method.toUpperCase()} ${url}`, { data });
    }
  }

  /**
   * Log API responses (development only)
   */
  logResponse(method: string, url: string, status: number, data?: unknown): void {
    if (this.isDevelopment) {
      this.debug(`API ${method.toUpperCase()} ${url} - ${status}`, { data });
    }
  }
}

export const logger = new Logger();
