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

const logger = new Logger('BillingService');

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(BillingModule, {
    // Enable raw body for webhook signature verification
    rawBody: true,
  });

  const config = app.get(ConfigService);
  const port = config.get<number>('BILLING_SERVICE_PORT', 3010);

  app.enableCors();

  await app.listen(port);
  logger.log(`billing-service running on port ${port}`);
}

bootstrap().catch((err) => {
  logger.error(`Failed to start billing-service: ${err.message}`, err.stack);
  process.exit(1);
});
