import { NestFactory } from '@nestjs/core';
import { NestExpressApplication } from '@nestjs/platform-express';
import { join } from 'path';

import { AppModule } from './app.module';

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

  /*
  |--------------------------------------------------------------------------
  | STATIC STORAGE
  |--------------------------------------------------------------------------
  | Expose:
  | /storage/documents/xxx.pdf
  |--------------------------------------------------------------------------
  */

  app.useStaticAssets(
    join(process.cwd(), 'storage'),
    {
      prefix: '/storage/',
    },
  );

  /*
  |--------------------------------------------------------------------------
  | CORS
  |--------------------------------------------------------------------------
  */

  app.enableCors({
    origin: true,
    credentials: true,
  });

  /*
  |--------------------------------------------------------------------------
  | GLOBAL PREFIX (optional later)
  |--------------------------------------------------------------------------
  */

  // app.setGlobalPrefix('api');

  /*
  |--------------------------------------------------------------------------
  | START SERVER
  |--------------------------------------------------------------------------
  */

  const port = Number(process.env.PORT || 3000);

  await app.listen(port);

  console.log(`✅ Backend running on port ${port}`);
}

bootstrap();