import { Module } from '@nestjs/common';
import { OutboxModule } from '../outbox/outbox.module';
import { MessageModule } from '../messaging/message.module';
import { NotificationController } from './notification.controller';
import { NotificationProjector } from './notification.projector';
import { WebPushDelivery } from './web-push.delivery';
import { NOTIFICATION_PORT } from './notification.port';

@Module({
  imports: [OutboxModule, MessageModule], // OutboxModule → bus/dlq; MessageModule → PresenceService. Prisma/Projection/Identity are @Global.
  controllers: [NotificationController],
  providers: [
    NotificationProjector,
    {
      // Dev binds Web Push (VAPID); prod can swap this to an email/FCM adapter.
      provide: NOTIFICATION_PORT,
      useFactory: () =>
        new WebPushDelivery(
          process.env.VAPID_PUBLIC_KEY
            ? { publicKey: process.env.VAPID_PUBLIC_KEY, privateKey: process.env.VAPID_PRIVATE_KEY ?? '', subject: process.env.VAPID_SUBJECT ?? 'mailto:dev@insignia' }
            : undefined,
        ),
    },
  ],
})
export class NotificationModule {}
