import { ContainerModule } from 'inversify';
import { EnhancedErrorReporter } from '../../infrastructure/EnhancedErrorReporter';
import { ConsoleLogger } from '../../infrastructure/logging/ConsoleLogger';
import { getWebsiteApiBaseUrl } from '../../config/apiBaseUrl';
import {
  LOGGER_TOKEN,
  ERROR_REPORTER_TOKEN,
  CONFIG_TOKEN
} from '../tokens';

export const CoreModule = new ContainerModule((options) => {
  const bind = options.bind;
  
  // Logger
  bind<ConsoleLogger>(LOGGER_TOKEN)
    .to(ConsoleLogger)
    .inSingletonScope();

  // Error Reporter
  bind<EnhancedErrorReporter>(ERROR_REPORTER_TOKEN)
    .toDynamicValue((context) => {
      const logger = context.get<ConsoleLogger>(LOGGER_TOKEN);
      return new EnhancedErrorReporter(logger, {
        showUserNotifications: true,
        logToConsole: true,
        reportToExternal: process.env.NODE_ENV === 'production',
      });
    })
    .inSingletonScope();

  // Config - bind as function to read env at runtime
  bind<() => string>(CONFIG_TOKEN)
    .toDynamicValue(() => () => getWebsiteApiBaseUrl());
});