/**
 * Error Reporting Utility
 * 
 * Provides centralized error reporting that respects environment settings.
 * In development: logs to console
 * In production: sends to error reporting service (when configured)
 */

type ErrorContext = Record<string, unknown>;

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

  /**
   * Log error with context
   */
  error(message: string, error?: unknown, context?: ErrorContext): void {
    if (this.isDevelopment) {
      console.error(`[ERROR] ${message}`, error, context);
    } else {
      // In production, send to error reporting service
      // TODO: Integrate with error reporting service (Sentry, LogRocket, etc.)
      // Example:
      // if (window.Sentry) {
      //   window.Sentry.captureException(error || new Error(message), {
      //     extra: context,
      //   });
      // }
      
      // For now, silently log to prevent information disclosure
      // In production, errors should be reported to a monitoring service
    }
  }

  /**
   * Log warning
   */
  warn(message: string, context?: ErrorContext): void {
    if (this.isDevelopment) {
      console.warn(`[WARN] ${message}`, context);
    }
    // Warnings typically don't need to be reported in production
  }

  /**
   * Log info message
   */
  info(message: string, context?: ErrorContext): void {
    if (this.isDevelopment) {
      console.info(`[INFO] ${message}`, context);
    }
  }

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

  /**
   * Log security warning (always shown, but muted in production)
   */
  securityWarning(message: string, context?: ErrorContext): void {
    if (this.isDevelopment) {
      console.warn(`[SECURITY WARNING] ${message}`, context);
    } else {
      // Security warnings should be logged to monitoring service in production
      // but not shown to users
      this.error(`Security Warning: ${message}`, undefined, context);
    }
  }
}

export const errorReporter = new ErrorReporter();

// Export convenience methods
export const logError = (message: string, error?: unknown, context?: ErrorContext) => 
  errorReporter.error(message, error, context);

export const logWarn = (message: string, context?: ErrorContext) => 
  errorReporter.warn(message, context);

export const logInfo = (message: string, context?: ErrorContext) => 
  errorReporter.info(message, context);

export const logDebug = (message: string, context?: ErrorContext) => 
  errorReporter.debug(message, context);

export const logSecurityWarning = (message: string, context?: ErrorContext) => 
  errorReporter.securityWarning(message, context);

export default errorReporter;

