import { Module, OnModuleInit } from '@nestjs/common';
import { WhatsAppWebhookController } from './whatsapp.controller';
import { QuantityWebviewController } from './quantity-webview.controller';
import { WhatsAppService } from './services/whatsapp.service';
import { WhatsAppBotEngine } from './services/whatsapp-bot.engine';
import { WhatsAppSessionService } from './services/whatsapp-session.service';
import { CommerceModule } from '../commerce/commerce.module';
import { PaymentModule } from '../payments/payment.module';
import { PaymentPollingService } from '../payments/payment-polling.service';

@Module({
  imports: [CommerceModule, PaymentModule],
  providers: [WhatsAppService, WhatsAppBotEngine, WhatsAppSessionService],
  controllers: [WhatsAppWebhookController, QuantityWebviewController],
  exports: [WhatsAppService, WhatsAppBotEngine],
})
export class WhatsAppModule implements OnModuleInit {
  constructor(
    private readonly whatsappService: WhatsAppService,
    private readonly pollingService: PaymentPollingService,
  ) {}

  onModuleInit() {
    // Wire WhatsApp service into the polling service to break circular dependency
    this.pollingService.registerWhatsAppService(this.whatsappService);
  }
}
