import { ConfigService } from '@nestjs/config';
import { MongooseModuleOptions } from '@nestjs/mongoose';

export const createMongooseOptions = (configService: ConfigService): MongooseModuleOptions => {
  const nodeEnv = configService.get<string>('nodeEnv', { infer: true }) ?? 'development';
  return {
    uri: configService.get<string>('mongodb.uri', { infer: true }),
    autoIndex:
      configService.get<boolean>('mongodb.autoIndex', { infer: true }) ?? nodeEnv !== 'production',
    minPoolSize: configService.get<number>('mongodb.minPoolSize', { infer: true }) ?? 5,
    maxPoolSize: configService.get<number>('mongodb.maxPoolSize', { infer: true }) ?? 100,
    maxIdleTimeMS: configService.get<number>('mongodb.maxIdleTimeMs', { infer: true }) ?? 60_000,
    serverSelectionTimeoutMS:
      configService.get<number>('mongodb.serverSelectionTimeoutMs', { infer: true }) ?? 10_000,
    socketTimeoutMS: configService.get<number>('mongodb.socketTimeoutMs', { infer: true }) ?? 45_000,
  };
};
