// Dezky platform API — Entry point.
// Owns the platform control plane: tenants, partners, users, subscriptions,
// plus the provisioning orchestration (Authentik / Stalwart / OCIS).

import { ValidationPipe } from '@nestjs/common'
import { NestFactory } from '@nestjs/core'
import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify'
import { AppModule } from './app.module.js'

async function bootstrap() {
  const app = await NestFactory.create<NestFastifyApplication>(
    AppModule,
    new FastifyAdapter({ logger: true }),
    // rawBody exposes req.rawBody (Buffer) — needed for Stripe webhook
    // signature verification, which must hash the exact bytes Stripe sent.
    { rawBody: true },
  )

  app.enableCors({
    origin: /^https:\/\/([a-z0-9-]+\.)?dezky\.local$/,
    credentials: true,
  })

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

  const port = Number(process.env.PORT ?? 3001)
  await app.listen(port, '0.0.0.0')

  console.log(`platform-api listening on http://0.0.0.0:${port}`)
}

bootstrap().catch((err) => {
  console.error('Failed to start platform-api', err)
  process.exit(1)
})
