import { ConsoleLogger } from '../../../../shared/logging/ConsoleLogger';
import { LogContext } from '../../../application/ports/LoggerContext';
import { LoggerPort } from '../../../application/ports/LoggerPort';

export class ConsoleLogAdapter implements LoggerPort {
  private consoleLogger: ConsoleLogger;
  private readonly context: LogContext;

  constructor(context: LogContext = {}) {
    this.consoleLogger = new ConsoleLogger();
    this.context = context;
  }

  debug(message: string, context?: LogContext): void {
    this.consoleLogger.debug(message, { ...this.context, ...context });
  }

  info(message: string, context?: LogContext): void {
    this.consoleLogger.info(message, { ...this.context, ...context });
  }

  warn(message: string, context?: LogContext): void {
    this.consoleLogger.warn(message, { ...this.context, ...context });
  }

  error(message: string, error?: Error, context?: LogContext): void {
    this.consoleLogger.error(message, error, { ...this.context, ...context });
  }

  fatal(message: string, error?: Error, context?: LogContext): void {
    this.consoleLogger.error(`FATAL: ${message}`, error, { ...this.context, ...context });
  }

  child(context: LogContext = {}): LoggerPort {
    return new ConsoleLogAdapter({ ...this.context, ...context });
  }

  async flush(): Promise<void> {
    // No-op for console logger as it's synchronous
    return Promise.resolve();
  }
}
