import './prototype';

import { ValidationPipe } from '@nestjs/common';
import { HttpAdapterHost, NestFactory } from '@nestjs/core';
import { NestExpressApplication } from '@nestjs/platform-express';
import { GlobalExceptionsFilter } from './common/filters/global-exception.filter';
import { ResponseInterceptor } from './common/interceptor/response.interceptor';
import { AppModule } from './app.module';
import env from './env';
import { initPlugins } from './common/plugins';
import * as fs from 'fs';
import { json } from 'express';

async function bootstrap() {
  const httpsOptions = null;
  console.log('httpsOptions', httpsOptions);
  // const app = initPlugins(
  //   await NestFactory.create<NestExpressApplication>(AppModule, {
  //     httpsOptions: !process.argv.includes('ENV=DEV')
  //       ? {
  //           key: fs.readFileSync('../192.168.0.32_3000_key'),
  //           cert: fs.readFileSync('../192.168.0.32_3000_ssl.crt'),
  //         }
  //       : null,
  //   }),
  // );
  const app = initPlugins(
    await NestFactory.create<NestExpressApplication>(AppModule),
  );
  app.use(json({ limit: '500mb' }));
  app.useGlobalPipes(new ValidationPipe());
  app.useGlobalInterceptors(new ResponseInterceptor());
  app.useGlobalFilters(new GlobalExceptionsFilter(app.get(HttpAdapterHost)));

  await app.listen(env.serve.port);
}

bootstrap().then(() => {
  console.log(
    `
    \n\x1B[42m NEST \x1B[0m Compiled successfully
    \n\x1B[32m➜ \x1B[0m Local:    \x1B[4m\x1B[34mhttp://localhost:${env.serve.port}\x1B[0m
    \n\x1B[32m➜ \x1B[0m Docs:     \x1B[4m\x1B[34mhttp://localhost:${env.serve.port}/${env.swagger.path}\x1B[0m
    \n\x1B[32m➜ \x1B[0m Init DB:  \x1B[4m\x1B[34mhttp://localhost:${env.serve.port}/initialize\x1B[0m
    `,
  );
});
