import { NestFactory } from '@nestjs/core';
import { NestExpressApplication } from '@nestjs/platform-express';
import { Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { VersionModule } from './version.module';

const logger = new Logger('VersionService');

process.on('unhandledRejection', (reason) => {
  logger.error(`Unhandled Rejection: ${reason}`);
});
process.on('uncaughtException', (error) => {
  logger.error(`Uncaught Exception: ${error.message}`, error.stack);
});

async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(VersionModule);
  const config = app.get(ConfigService);
  const port = config.get<number>('VERSION_SERVICE_PORT', 3009);
  // Serve uploaded APK/IPA files as static assets
  // Files are stored at /data/versions/{platform}/filename
  // Accessible via GET /downloads/versions/{platform}/filename
  app.useStaticAssets('/data/versions', { prefix: '/downloads/versions' });
  await app.listen(port);
  logger.log(`version-service running on port ${port}`);
}
bootstrap().catch((err) => {
  logger.error(`Failed to start version-service: ${err.message}`, err.stack);
  process.exit(1);
});
