import { Module } from '@nestjs/common';
import { CqrsModule } from '@nestjs/cqrs';
import { MulterModule } from '@nestjs/platform-express';
import { CreateListingHandler } from './application/commands/create-listing/create-listing.handler';
import { FeatureListingHandler } from './application/commands/feature-listing/feature-listing.handler';
import { ModerateListingHandler } from './application/commands/moderate-listing/moderate-listing.handler';
import { UpdateListingHandler } from './application/commands/update-listing/update-listing.handler';
import { UpdateListingStatusHandler } from './application/commands/update-listing-status/update-listing-status.handler';
import { UploadMediaHandler } from './application/commands/upload-media/upload-media.handler';
import { ActivateFeaturedListingHandler } from './application/event-handlers/activate-featured-listing.handler';
import { RecordPriceHistoryHandler } from './application/event-handlers/record-price-history.handler';
import { GetListingHandler } from './application/queries/get-listing/get-listing.handler';
import { GetPendingModerationHandler } from './application/queries/get-pending-moderation/get-pending-moderation.handler';
import { GetPriceHistoryHandler } from './application/queries/get-price-history/get-price-history.handler';
import { SearchListingsHandler } from './application/queries/search-listings/search-listings.handler';
import { LISTING_REPOSITORY } from './domain/repositories/listing.repository';
import { PROPERTY_REPOSITORY } from './domain/repositories/property.repository';
import { DUPLICATE_DETECTOR } from './domain/services/duplicate-detector';
import { ModerationService } from './domain/services/moderation.service';
import { PRICE_VALIDATOR } from './domain/services/price-validator';
import { PrismaListingRepository } from './infrastructure/repositories/prisma-listing.repository';
import { PrismaPropertyRepository } from './infrastructure/repositories/prisma-property.repository';
import { MEDIA_STORAGE_SERVICE, MinioMediaStorageService } from './infrastructure/services/media-storage.service';
import { PrismaDuplicateDetector } from './infrastructure/services/prisma-duplicate-detector';
import { PrismaPriceValidator } from './infrastructure/services/prisma-price-validator';
import { ListingsController } from './presentation/controllers/listings.controller';

const CommandHandlers = [
  CreateListingHandler,
  FeatureListingHandler,
  UpdateListingHandler,
  UpdateListingStatusHandler,
  UploadMediaHandler,
  ModerateListingHandler,
];

const QueryHandlers = [
  GetListingHandler,
  SearchListingsHandler,
  GetPendingModerationHandler,
  GetPriceHistoryHandler,
];

const EventHandlers = [
  ActivateFeaturedListingHandler,
  RecordPriceHistoryHandler,
];

@Module({
  imports: [
    CqrsModule,
    MulterModule.register({
      limits: { fileSize: 100 * 1024 * 1024 }, // 100 MB — per-type limits enforced by FileValidationPipe
    }),
  ],
  controllers: [ListingsController],
  providers: [
    // Repositories
    { provide: PROPERTY_REPOSITORY, useClass: PrismaPropertyRepository },
    { provide: LISTING_REPOSITORY, useClass: PrismaListingRepository },

    // Services
    { provide: DUPLICATE_DETECTOR, useClass: PrismaDuplicateDetector },
    { provide: PRICE_VALIDATOR, useClass: PrismaPriceValidator },
    ModerationService,
    { provide: MEDIA_STORAGE_SERVICE, useClass: MinioMediaStorageService },

    // CQRS
    ...CommandHandlers,
    ...QueryHandlers,
    ...EventHandlers,
  ],
  exports: [LISTING_REPOSITORY, PROPERTY_REPOSITORY],
})
export class ListingsModule {}
