import { Logger, ValidationPipe } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { NestFactory } from '@nestjs/core';
import { MicroserviceOptions, Transport } from '@nestjs/microservices';
import { join } from 'node:path';
import { AppModule } from './app.module';

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

  app.useGlobalPipes(
    new ValidationPipe({
      whitelist: true,
      transform: true,
      forbidNonWhitelisted: true
    })
  );

  app.connectMicroservice<MicroserviceOptions>({
    transport: Transport.GRPC,
    options: {
      package: ['auth', 'admin', 'rbac', 'security', 'profile', 'documents', 'addresses', 'identity', 'media', 'notifications', 'chat', 'bot'],
      protoPath: [
        join(__dirname, '../../../shared/proto/auth.proto'),
        join(__dirname, '../../../shared/proto/admin.proto'),
        join(__dirname, '../../../shared/proto/rbac.proto'),
        join(__dirname, '../../../shared/proto/security.proto'),
        join(__dirname, '../../../shared/proto/profile.proto'),
        join(__dirname, '../../../shared/proto/documents.proto'),
        join(__dirname, '../../../shared/proto/addresses.proto'),
        join(__dirname, '../../../shared/proto/identity.proto'),
        join(__dirname, '../../../shared/proto/media.proto'),
        join(__dirname, '../../../shared/proto/notifications.proto'),
        join(__dirname, '../../../shared/proto/chat.proto'),
        join(__dirname, '../../../shared/proto/bot.proto')
      ],
      url: config.getOrThrow<string>('GRPC_URL')
    }
  });

  await app.startAllMicroservices();
  await app.listen(config.get<number>('HTTP_PORT', 3001));
  Logger.log('SSO Core запущен', 'Bootstrap');
}

void bootstrap();
