import { Module } from '@nestjs/common';
import { OutboxModule } from '../outbox/outbox.module';
import { SearchService } from './search.service';
import { SearchProjector } from './search.projector';
import { SearchController } from './search.controller';
import { MESSAGE_SEARCH_PORT } from './message-search.port';
import { messageSearchFromEnv } from './meili.adapter';

/**
 * Message search (Meilisearch). The engine binds from MEILI_URL; unset → a no-op port so search
 * returns empty rather than erroring. The projector indexes on `message.sent`; the service enforces
 * the permission fence (scope + caller's threads). SessionVerifier + ActorResolver are global.
 */
@Module({
  imports: [OutboxModule],
  controllers: [SearchController],
  providers: [
    SearchService,
    SearchProjector,
    { provide: MESSAGE_SEARCH_PORT, useFactory: () => messageSearchFromEnv() },
  ],
  exports: [SearchService],
})
export class SearchModule {}
