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

const logger = new Logger('OpsService');

// 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(OpsModule);
  const config = app.get(ConfigService);
  const port = config.get<number>('OPS_SERVICE_PORT', 3003);
  await app.listen(port);
  logger.log(`ops-service running on port ${port}`);
}
bootstrap().catch((err) => {
  logger.error(`Failed to start ops-service: ${err.message}`, err.stack);
  process.exit(1);
});
