/**
 * 统一日志服务
 * 提供标准化的日志格式和方法
 */

interface LogContext {
  service?: string;
  method?: string;
  tenantId?: string;
  shopId?: string;
  taskId?: string;
  traceId?: string;
  businessType?: 'TOC' | 'TOB';
  [key: string]: any;
}

interface LogOptions {
  context?: LogContext;
  error?: Error;
  data?: any;
}

export class LogService {
  /**
   * 生成标准日志格式
   * @param level 日志级别
   * @param message 日志消息
   * @param options 日志选项
   */
  private static formatLog(level: string, message: string, options: LogOptions = {}): string {
    const timestamp = new Date().toISOString();
    const context = options.context || {};
    const data = options.data;
    const error = options.error;

    // 构建日志对象
    const logObj = {
      timestamp,
      level,
      message,
      context,
      ...(data && { data }),
      ...(error && {
        error: {
          message: error.message,
          stack: error.stack
        }
      })
    };

    return JSON.stringify(logObj);
  }

  /**
   * 信息日志
   * @param message 日志消息
   * @param options 日志选项
   */
  static info(message: string, options: LogOptions = {}): void {
    const formattedLog = this.formatLog('INFO', message, options);
    console.log(formattedLog);
  }

  /**
   * 错误日志
   * @param message 日志消息
   * @param options 日志选项
   */
  static error(message: string, options: LogOptions = {}): void {
    const formattedLog = this.formatLog('ERROR', message, options);
    console.error(formattedLog);
  }

  /**
   * 警告日志
   * @param message 日志消息
   * @param options 日志选项
   */
  static warn(message: string, options: LogOptions = {}): void {
    const formattedLog = this.formatLog('WARN', message, options);
    console.warn(formattedLog);
  }

  /**
   * 调试日志
   * @param message 日志消息
   * @param options 日志选项
   */
  static debug(message: string, options: LogOptions = {}): void {
    if (process.env.NODE_ENV === 'development') {
      const formattedLog = this.formatLog('DEBUG', message, options);
      console.debug(formattedLog);
    }
  }

  /**
   * 追踪日志
   * @param message 日志消息
   * @param options 日志选项
   */
  static trace(message: string, options: LogOptions = {}): void {
    if (process.env.NODE_ENV === 'development') {
      const formattedLog = this.formatLog('TRACE', message, options);
      console.trace(formattedLog);
    }
  }

  /**
   * 创建服务特定的日志实例
   * @param serviceName 服务名称
   */
  static createLogger(serviceName: string) {
    return {
      info: (message: string, options: Omit<LogOptions, 'context'> & { context?: Omit<LogContext, 'service'> } = {}) => {
        this.info(message, {
          ...options,
          context: {
            service: serviceName,
            ...options.context
          }
        });
      },
      error: (message: string, options: Omit<LogOptions, 'context'> & { context?: Omit<LogContext, 'service'> } = {}) => {
        this.error(message, {
          ...options,
          context: {
            service: serviceName,
            ...options.context
          }
        });
      },
      warn: (message: string, options: Omit<LogOptions, 'context'> & { context?: Omit<LogContext, 'service'> } = {}) => {
        this.warn(message, {
          ...options,
          context: {
            service: serviceName,
            ...options.context
          }
        });
      },
      debug: (message: string, options: Omit<LogOptions, 'context'> & { context?: Omit<LogContext, 'service'> } = {}) => {
        this.debug(message, {
          ...options,
          context: {
            service: serviceName,
            ...options.context
          }
        });
      },
      trace: (message: string, options: Omit<LogOptions, 'context'> & { context?: Omit<LogContext, 'service'> } = {}) => {
        this.trace(message, {
          ...options,
          context: {
            service: serviceName,
            ...options.context
          }
        });
      }
    };
  }
}

export default LogService;
