/**
 * Copyright 2024 Phenix Real Time Solutions, Inc. Confidential and Proprietary. All Rights Reserved.
 */
import PlatformDetectionService from 'services/platform-detection.service';
import hostService from 'services/url.service';
import TelemetryConfiguration from 'services/telemetry/TelemetryConfiguration';
import TelemetryAppender from 'services/telemetry/TelemetryApender';
// import userStore from 'services/user-store';
import {ILogger} from './LoggerInterface';
import Logger, {LoggingLevel} from './Logger';
import Appenders from './Appenders';
import LoggingThreshold from './LoggingThreshold';
import ConsoleAppender from './ConsoleAppender';
import LoggerDefaults from './LoggerDefaults';

export default class LoggerFactory {
  private static _loggers: {[category: string]: ILogger} = {};
  private static _appenders: Appenders = new Appenders();
  private static _threshold: LoggingThreshold = new LoggingThreshold();
  private static _telemetryConfiguration: TelemetryConfiguration = new TelemetryConfiguration();
  private static _initialized: boolean = false;

  static get telemetryConfiguration(): TelemetryConfiguration {
    return this._telemetryConfiguration;
  }

  static applyLoggerConfig(): void {
    if (this._initialized) return;

    LoggerFactory.applyConsoleLogger(LoggingLevel['All']);
    LoggerFactory.applyLoggingLevel();
    LoggerFactory.applyTelemetryLogger();
    this._initialized = true;
  }

  static getLogger(category: string): ILogger {
    if (typeof category !== 'string') {
      category = 'portal';
    }

    const logger = LoggerFactory._loggers[category];

    if (logger) {
      return logger;
    }

    return (LoggerFactory._loggers[category] = new Logger(category, this._appenders, this._threshold));
  }

  static applyLoggingLevel(): void {
    this._threshold.setThreshold(LoggingLevel['All']);
  }

  static applyConsoleLogger(level: LoggingLevel): void {
    this._appenders.add(new ConsoleAppender(level || LoggerDefaults.defaultConsoleLoggingLevel));
  }

  static async applyTelemetryConfiguration(level: LoggingLevel): Promise<void> {
    this._telemetryConfiguration.threshold = level || LoggerDefaults.defaultTelemetryLoggingLevel;
    this._telemetryConfiguration.url = hostService.getTelemetryUrl();
    this._telemetryConfiguration.environment = hostService.getPcastBaseUrlOrigin();
    // this._telemetryConfiguration.tenancy = await userStore.get('applicationId');
    // this._telemetryConfiguration.userId = await userStore.get('applicationId');
    // this._telemetryConfiguration.sessionId = await userStore.get('sessionId');

    // Lazy access to PlatformDetectionService to avoid circular dependency
    try {
      this._telemetryConfiguration.browser = PlatformDetectionService.browser
        ? `${PlatformDetectionService.browser}/${PlatformDetectionService.version}`
        : 'unknown';
    } catch (error) {
      console.warn('Failed to get platform detection info for telemetry:', error);
      this._telemetryConfiguration.browser = 'unknown';
    }
  }

  private static applyTelemetryLogger(): void {
    LoggerFactory.applyTelemetryConfiguration(LoggingLevel['Info']);

    this._appenders.add(new TelemetryAppender(this._telemetryConfiguration));
  }

  private constructor() {
    throw new Error('LoggerFactory is a static class that may not be instantiated');
  }
}
