import { NestFactory, Reflector } from '@nestjs/core';
import { ValidationPipe, ClassSerializerInterceptor } from '@nestjs/common';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  // Allow requests from the Expo dev server and the mobile app
  app.enableCors();
  app.setGlobalPrefix('api');

  // Exclude @Exclude()-marked fields (e.g. circular back-references) from responses
  app.useGlobalInterceptors(new ClassSerializerInterceptor(app.get(Reflector)));

  // Global validation pipe — transform only, no whitelist stripping.
  // Auth routes use Zod for validation so we must not strip unknown fields here.
  app.useGlobalPipes(
    new ValidationPipe({
      transform: true,
    }),
  );

  const port = process.env['PORT'] ?? 3000;
  await app.listen(port);
  console.log(`LexiBrain API running on port ${port}`);
}

bootstrap();
