import { Logger, ILogObj } from 'tslog'
import { Service } from '../core/Service.js'

export enum LogLevel {
  Silly = 0,
  Trace = 1,
  Debug = 2,
  Info = 3,
  Warn = 4,
  Error = 5,
  Fatal = 6,
}

export default class LoggingService extends Service {
  static override key = 'api-service/logger'

  logger!: Logger<ILogObj>

  /**
   * 0: silly, 1: trace, 2: debug, 3: info, 4: warn, 5: error, 6: fatal
   */
  level = LogLevel.Error

  constructor(env = 'production') {
    super()
    this.level = env === 'production' ? LogLevel.Trace : LogLevel.Error
  }

  protected override async onCreate(): Promise<void> {
    this.logger = new Logger({ name: 'quick-api', type: 'pretty', minLevel: this.level })
  }

  silly(...args: unknown[]): void {
    this.logger.silly(...args)
  }

  debug(...args: unknown[]): void {
    this.logger.debug(...args)
  }

  info(...args: unknown[]): void {
    this.logger.info(...args)
  }

  warn(...args: unknown[]): void {
    this.logger.warn(...args)
  }

  error(...args: unknown[]): void {
    this.logger.error(...args)
  }

  fatal(...args: unknown[]): void {
    this.logger.fatal(...args)
  }

  trace(...args: unknown[]): void {
    this.logger.trace(...args)
  }
}
