import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';

import { Lead } from './entities/lead.entity';
import { LeadActivity } from './entities/lead-activity.entity';
import { User } from '../user/entities/user.entity';

import { LeadService } from './services/lead.service';
import { DuplicateLeadDetectorService } from './services/duplicate-lead-detector.service';
import {
  LeadNotificationsService,
  NOTIFICATIONS_PORT,
  MAILS_PORT,
} from './services/lead-notifications.service';
import { LeadController } from './lead.controller';

// Same two modules audit-requests imports.
import { NotificationsModule } from '../notifications/notifications.module';
import { NotificationsService } from '../notifications/notifications.service';
import { MailsModule } from '../mails/mails.module';
import { MailsService } from '../mails/mails.service';

@Module({
  imports: [
    TypeOrmModule.forFeature([Lead, LeadActivity, User], 'scheme_dbs'),
    NotificationsModule,
    MailsModule,
  ],
  controllers: [LeadController],
  providers: [
    LeadService,
    DuplicateLeadDetectorService,
    LeadNotificationsService,

    // ── Port bindings ───────────────────────────────────────────────────
    // LeadNotificationsService depends on two narrow interfaces rather than
    // your concrete classes, so the lead module compiles even while the
    // real signatures are still being confirmed. Both modules must EXPORT
    // their service for useExisting to resolve.
    { provide: NOTIFICATIONS_PORT, useExisting: NotificationsService },
    { provide: MAILS_PORT, useExisting: MailsService },

    // If the real method names differ from create() / sendMail(), don't
    // edit the notifications service — remap right here instead:
    //
    // {
    //   provide: NOTIFICATIONS_PORT,
    //   inject: [NotificationsService],
    //   useFactory: (n: NotificationsService) => ({
    //     create: (p) => n.sendNotification({
    //       userId: p.user_id,
    //       type: p.type,
    //       title: p.title,
    //       body: p.message,
    //       linkUrl: p.link,
    //     }),
    //   }),
    // },
  ],
  exports: [LeadService, LeadNotificationsService],
})
export class LeadModule {}