import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { NestExpressApplication } from '@nestjs/platform-express';
import { ValidationPipe } from '@nestjs/common';
import cookieParser from 'cookie-parser';

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

  // CORS Konfiguration
  app.enableCors({
    origin: [
      'http://localhost:5173',     // Vite Dev Server
      'http://127.0.0.1:5173',     // Vite Dev Server (Alternative)
      'http://localhost:5500',
      'http://127.0.0.1:5500',
      'http://localhost',
      '*'
    ],
    credentials: true,
    methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'],
    allowedHeaders: ['Content-Type', 'Authorization'],
  });

  // Globales Präfix für alle Routes
  app.setGlobalPrefix('api');

  // Cookie Parser für JWT-Cookies
  app.use(cookieParser());

  // Validation Pipe für DTOs
  app.useGlobalPipes(
    new ValidationPipe({
      whitelist: true,
      forbidNonWhitelisted: true,
      transform: true,
    }),
  );

  await app.listen(process.env.PORT ?? 3000);
}
bootstrap();
