import pino, { Logger as PinoLoggerInstance } from 'pino';
import { ILogger } from '../../application/ports/logger';
import { env } from '../../main/config/env';

const createPinoInstance = (): PinoLoggerInstance => {
  const isProduction = env.NODE_ENV === 'production';

  return pino({
    level: env.LOG_LEVEL,
    base: {
      service: 'trust-wallet-server',
      env: env.NODE_ENV,
    },
    ...(isProduction
      ? {}
      : {
          transport: {
            target: 'pino-pretty',
            options: {
              colorize: true,
              translateTime: 'SYS:standard',
              ignore: 'pid,hostname',
            },
          },
        }),
  });
};

const rootLogger = createPinoInstance();

export class PinoLogger implements ILogger {
  private readonly logger: PinoLoggerInstance;

  constructor(logger?: PinoLoggerInstance) {
    this.logger = logger ?? rootLogger;
  }

  debug(message: string, context?: Record<string, unknown>): void {
    this.logger.debug(context ?? {}, message);
  }

  info(message: string, context?: Record<string, unknown>): void {
    this.logger.info(context ?? {}, message);
  }

  warn(message: string, context?: Record<string, unknown>): void {
    this.logger.warn(context ?? {}, message);
  }

  error(message: string, context?: Record<string, unknown>): void {
    this.logger.error(context ?? {}, message);
  }

  child(bindings: Record<string, unknown>): ILogger {
    return new PinoLogger(this.logger.child(bindings));
  }
}
