import { NestFactory } from '@nestjs/core';
import { Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { AuthModule } from './auth.module';

const logger = new Logger('AuthService');

// Prevent process crash from unhandled errors
process.on('unhandledRejection', (reason) => {
  logger.error(`Unhandled Rejection: ${reason}`);
});
process.on('uncaughtException', (error) => {
  logger.error(`Uncaught Exception: ${error.message}`, error.stack);
});

async function bootstrap() {
  const app = await NestFactory.create(AuthModule);
  const config = app.get(ConfigService);
  const port = config.get<number>('AUTH_SERVICE_PORT', 3001);
  await app.listen(port);
  logger.log(`auth-service running on port ${port}`);
}
bootstrap().catch((err) => {
  logger.error(`Failed to start auth-service: ${err.message}`, err.stack);
  process.exit(1);
});
