// OTel must be initialised before any other requires — do not move this line.
import './tracing';

import { Logger } from 'nestjs-pino';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app/app.module';

async function bootstrap() {
  if (process.env['NODE_ENV'] === 'production' && process.env['DEV_AUTH_BYPASS']) {
    throw new Error('DEV_AUTH_BYPASS must not be set in production');
  }

  const app = await NestFactory.create(AppModule, { bufferLogs: true });
  app.useLogger(app.get(Logger));

  const allowedOrigins =
    process.env['NODE_ENV'] === 'development'
      ? ['http://localhost:4200', /https:\/\/.*\.ext-twitch\.tv$/]
      : [/https:\/\/.*\.ext-twitch\.tv$/];
  app.enableCors({ origin: allowedOrigins, credentials: true });

  const globalPrefix = 'api';
  app.setGlobalPrefix(globalPrefix);

  const port = process.env['PORT'] ?? 3000;
  await app.listen(port, '0.0.0.0');

  app.get(Logger).log(
    `Application running on http://localhost:${port}/${globalPrefix}`,
    'bootstrap'
  );
}

bootstrap();
