import { environment } from '@env/environment';
import { LogLevel } from '@app/domain/log-level';
import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root',
})
export class LoggerService {
  private level: LogLevel = environment.production ? LogLevel.ERROR : LogLevel.DEBUG;

  debug(msg: string, ...optionalParams: any[]) {
    this.log(LogLevel.DEBUG, msg, optionalParams);
  }

  info(msg: string, ...optionalParams: any[]) {
    this.log(LogLevel.INFO, msg, optionalParams);
  }

  warn(msg: string, ...optionalParams: any[]) {
    this.log(LogLevel.WARN, msg, optionalParams);
  }

  error(msg: string, ...optionalParams: any[]) {
    this.log(LogLevel.ERROR, msg, optionalParams);
    // TODO: Ici, vous pourriez envoyer l'erreur vers votre backend PocketBase ou Sentry
    // this.sendToBackend(msg, optionalParams);
  }

  private log(level: LogLevel, msg: string, params: any[]) {
    if (level >= this.level) {
      switch (level) {
        case LogLevel.DEBUG:
          console.debug('%c[DEBUG]:', 'color: blue', msg, ...params);
          break;
        case LogLevel.INFO:
          console.info('%c[INFO]:', 'color: green', msg, ...params);
          break;
        case LogLevel.WARN:
          console.warn('%c[WARN]:', 'color: orange', msg, ...params);
          break;
        case LogLevel.ERROR:
          console.error('%c[ERROR]:', 'color: red', msg, ...params);
          break;
      }
    }
  }
}
