import morgan from 'morgan';
import { Logger, ValidationPipe, VersioningType } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './infrastructure/modules/app.module';
import { SwaggerModule, DocumentBuilder, SwaggerDocumentOptions } from '@nestjs/swagger';
import { ConfigService } from '@nestjs/config';
/**
 * Main entry point of the application
 * @returns Nothing
 */
async function bootstrap() {
  // Http Server
  const app = await NestFactory.create(AppModule);

  app.use(morgan('dev'));
  app.useGlobalPipes(
    new ValidationPipe({
      disableErrorMessages: false,
    })
  );

  /**
   * Enabling URI-type versioning of the API
   */
  app.enableVersioning({
    type: VersioningType.URI,
  });

  /**
   * Configuration of the Swagger document
   */
  const config = new DocumentBuilder()
    .setTitle('Freeland')
    .setDescription('Freeland open library API')
    .setVersion('0.0.1')
    .build();

  const document = SwaggerModule.createDocument(app, config, {
    deepScanRoutes: true,
  });
  SwaggerModule.setup('api', app, document);

  try {
    const configService = app.get(ConfigService);
    const NODE_PORT = configService.get('NODE_PORT');
    if (!NODE_PORT) {
      throw new Error('Please define the node port as an environmental variable');
    }

    /*const ES_PORT = configService.get('ES_PORT');
    if(!ES_PORT) {
      throw new Error('Please define the Elasticsearch port as an environmental variable');
    }*/

    await app.listen(NODE_PORT, () => Logger.log('HTTP Service is listening on port ' + String(NODE_PORT), 'App'));
  } catch (error) {
    console.log(error);
    
    return;
  }
}
//==================================================================================================================================
bootstrap();
