import { NestFactory } from '@nestjs/core'
import { AppModule } from './app.module'
import { Logger, ValidationPipe } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'
import { configureSwaggerDocs } from './helpers/configure-swagger-docs.helper'
import { NestExpressApplication } from '@nestjs/platform-express'
import { join } from 'path'

async function bootstrap() {
    const app = await NestFactory.create<NestExpressApplication>(AppModule, {
        snapshot: true,
        abortOnError: true
    })
    const configService = app.get<ConfigService>(ConfigService)

    app.setGlobalPrefix('api')
    app.useGlobalPipes(
        new ValidationPipe({
            whitelist: false,
            transform: true,
            forbidNonWhitelisted: false,
            transformOptions: {
                enableImplicitConversion: true
            }
        })
    )

    configureSwaggerDocs(app, configService)

    app.enableCors({
        origin: true,
        methods: 'GET,POST,PUT,PATCH,DELETE',
        credentials: true
    })
    app.useStaticAssets(join(__dirname, '..', 'public'))
    app.setBaseViewsDir(join(__dirname, '..', 'views'))
    app.setViewEngine('hbs')

    const port = configService.get<number>('NODE_API_PORT') || 3000
    await app.listen(port)
    Logger.log(`Url for OpenApi: ${await app.getUrl()}/docs`, 'Swagger')
}
bootstrap().catch((err) => {
    Logger.error(err, 'Bootstrap')
    process.exit(1)
})
