import { NestFactory } from '@nestjs/core';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { AppModule } from './app.module';
import { join } from 'path';
import * as express from 'express';

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

  app.use('/uploads', express.static(join(__dirname, '..', 'uploads')));

  app.setGlobalPrefix('');

  app.enableCors({
    origin: ['http://localhost:3000', "http://travelmarine.ru", "https://travelmarine.ru"],
    methods: 'GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS',
    credentials: true,
    allowedHeaders: 'Content-Type, Authorization, X-Requested-With',
    exposedHeaders: 'Authorization',
    maxAge: 86400,
  });

  const config = new DocumentBuilder()
    .setTitle('Travelmarine backend')
    .setDescription('Backend API for Travelmarine service')
    .setVersion('0.1')
    .addServer('http://localhost:4000', 'Local server')
    .addServer('http://89.169.188.2/api', 'Production server')
    .build();

  const documentFactory = () => SwaggerModule.createDocument(app, config);
  SwaggerModule.setup('/', app, documentFactory);

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