import { Module, type OnModuleInit } from '@nestjs/common';
import { CqrsModule } from '@nestjs/cqrs';
import { makeCounterProvider } from '@willsoto/nestjs-prometheus';
import { LoggerService, TypesenseClientService } from '@modules/shared';
import { SubscriptionsModule } from '@modules/subscriptions';
import { CreateSavedSearchHandler } from './application/commands/create-saved-search/create-saved-search.handler';
import { DeleteSavedSearchHandler } from './application/commands/delete-saved-search/delete-saved-search.handler';
import { ReindexAllHandler } from './application/commands/reindex-all/reindex-all.handler';
import { SyncListingHandler } from './application/commands/sync-listing/sync-listing.handler';
import { UpdateSavedSearchHandler } from './application/commands/update-saved-search/update-saved-search.handler';
import { GeoSearchHandler } from './application/queries/geo-search/geo-search.handler';
import { GetSavedSearchHandler } from './application/queries/get-saved-search/get-saved-search.handler';
import { GetSavedSearchesHandler } from './application/queries/get-saved-searches/get-saved-searches.handler';
import { SearchPropertiesHandler } from './application/queries/search-properties/search-properties.handler';
import { SEARCH_REPOSITORY } from './domain/repositories/search.repository';
import { SavedSearchAlertCronService } from './infrastructure/cron/saved-search-alert-cron.service';
import { ListingApprovedEventHandler } from './infrastructure/event-handlers/listing-approved.handler';
import { ListingFeaturedExpiredHandler } from './infrastructure/event-handlers/listing-featured-expired.handler';
import { ListingStatusChangedHandler } from './infrastructure/event-handlers/listing-status-changed.handler';
import { SavedSearchAlertHandler } from './infrastructure/event-handlers/saved-search-alert.handler';
import { ListingIndexerService } from './infrastructure/services/listing-indexer.service';
import { PostgresSearchRepository } from './infrastructure/services/postgres-search.repository';
import { ResilientSearchRepository, SEARCH_DEGRADATION_TOTAL } from './infrastructure/services/resilient-search.repository';
import { TypesenseSearchRepository } from './infrastructure/services/typesense-search.repository';
import { SavedSearchController } from './presentation/controllers/saved-search.controller';
import { SearchController } from './presentation/controllers/search.controller';

const CommandHandlers = [SyncListingHandler, ReindexAllHandler, CreateSavedSearchHandler, DeleteSavedSearchHandler, UpdateSavedSearchHandler];
const QueryHandlers = [SearchPropertiesHandler, GeoSearchHandler, GetSavedSearchesHandler, GetSavedSearchHandler];

@Module({
  imports: [CqrsModule, SubscriptionsModule],
  controllers: [SearchController, SavedSearchController],
  providers: [
    // Infrastructure
    TypesenseSearchRepository,
    PostgresSearchRepository,
    ResilientSearchRepository,
    { provide: SEARCH_REPOSITORY, useExisting: ResilientSearchRepository },
    ListingIndexerService,

    // Metrics
    makeCounterProvider({
      name: SEARCH_DEGRADATION_TOTAL,
      help: 'Total search degradation events (Typesense circuit breaker)',
      labelNames: ['service', 'event'],
    }),

    // Event handlers
    ListingApprovedEventHandler,
    ListingFeaturedExpiredHandler,
    ListingStatusChangedHandler,
    SavedSearchAlertHandler,

    // Cron jobs
    SavedSearchAlertCronService,

    // CQRS
    ...CommandHandlers,
    ...QueryHandlers,
  ],
  exports: [ListingIndexerService, SEARCH_REPOSITORY],
})
export class SearchModule implements OnModuleInit {
  constructor(
    private readonly searchRepo: ResilientSearchRepository,
    private readonly logger: LoggerService,
  ) {}

  async onModuleInit(): Promise<void> {
    try {
      await this.searchRepo.ensureCollection();
      this.logger.log('Search module initialized — Typesense collection ready', 'SearchModule');
    } catch (err) {
      this.logger.warn(
        `Typesense collection initialization failed: ${err instanceof Error ? err.message : String(err)} — PostgreSQL fallback is active`,
        'SearchModule',
      );
    }
  }
}
