import { Module } from '@nestjs/common';
import { CqrsModule } from '@nestjs/cqrs';
import { GenerateReportHandler } from './application/commands/generate-report/generate-report.handler';
import { TrackEventHandler } from './application/commands/track-event/track-event.handler';
import { UpdateMarketIndexHandler } from './application/commands/update-market-index/update-market-index.handler';
import { ListingCreatedModerationHandler } from './application/event-handlers/listing-created-moderation.handler';
import { GetDistrictStatsHandler } from './application/queries/get-district-stats/get-district-stats.handler';
import { GetHeatmapHandler } from './application/queries/get-heatmap/get-heatmap.handler';
import { GetMarketReportHandler } from './application/queries/get-market-report/get-market-report.handler';
import { GetPriceTrendHandler } from './application/queries/get-price-trend/get-price-trend.handler';
import { GetValuationHandler } from './application/queries/get-valuation/get-valuation.handler';
import { MARKET_INDEX_REPOSITORY } from './domain/repositories/market-index.repository';
import { VALUATION_REPOSITORY } from './domain/repositories/valuation.repository';
import { AVM_SERVICE } from './domain/services/avm-service';
import { PrismaMarketIndexRepository } from './infrastructure/repositories/prisma-market-index.repository';
import { PrismaValuationRepository } from './infrastructure/repositories/prisma-valuation.repository';
import { AI_SERVICE_CLIENT, AiServiceClient } from './infrastructure/services/ai-service.client';
import { HttpAVMService } from './infrastructure/services/http-avm.service';
import { MarketIndexCronService } from './infrastructure/services/market-index-cron.service';
import { PrismaAVMService } from './infrastructure/services/prisma-avm.service';
import { AnalyticsController } from './presentation/controllers/analytics.controller';

const CommandHandlers = [
  TrackEventHandler,
  GenerateReportHandler,
  UpdateMarketIndexHandler,
];

const QueryHandlers = [
  GetMarketReportHandler,
  GetHeatmapHandler,
  GetPriceTrendHandler,
  GetDistrictStatsHandler,
  GetValuationHandler,
];

const EventHandlers = [
  ListingCreatedModerationHandler,
];

@Module({
  imports: [CqrsModule],
  controllers: [AnalyticsController],
  providers: [
    // AI service client
    { provide: AI_SERVICE_CLIENT, useClass: AiServiceClient },

    // Repositories
    { provide: MARKET_INDEX_REPOSITORY, useClass: PrismaMarketIndexRepository },
    { provide: VALUATION_REPOSITORY, useClass: PrismaValuationRepository },

    // AVM: HttpAVMService calls Python AI first, falls back to PrismaAVMService
    PrismaAVMService,
    { provide: AVM_SERVICE, useClass: HttpAVMService },

    // Cron
    MarketIndexCronService,

    // CQRS
    ...CommandHandlers,
    ...QueryHandlers,
    ...EventHandlers,
  ],
  exports: [MARKET_INDEX_REPOSITORY, VALUATION_REPOSITORY, AVM_SERVICE, AI_SERVICE_CLIENT],
})
export class AnalyticsModule {}
