import { Module } from '@nestjs/common';
import { APP_INTERCEPTOR } from '@nestjs/core';
import { ConfigModule } from '@nestjs/config';
import { ServeStaticModule } from '@nestjs/serve-static';
import { ScheduleModule } from '@nestjs/schedule';
import { join } from 'path';
import { configurations } from './config';
import { PrismaService } from './infrastructure/persistence/prisma/prisma.service';
import { AppVersionMapper } from './infrastructure/persistence/mappers/app-version.mapper';
import { AppVersionRepositoryImpl } from './infrastructure/persistence/repositories/app-version.repository.impl';
import { APP_VERSION_REPOSITORY } from './domain/repositories/app-version.repository';
import { FileStorageService } from './infrastructure/storage/file-storage.service';
import { PackageParserService } from './infrastructure/parsers/package-parser.service';
import { CheckUpdateHandler } from './application/queries/check-update/check-update.handler';
import { ListVersionsHandler } from './application/queries/list-versions/list-versions.handler';
import { GetVersionHandler } from './application/queries/get-version/get-version.handler';
import { CreateVersionHandler } from './application/commands/create-version/create-version.handler';
import { UpdateVersionHandler } from './application/commands/update-version/update-version.handler';
import { DeleteVersionHandler } from './application/commands/delete-version/delete-version.handler';
import { ToggleVersionHandler } from './application/commands/toggle-version/toggle-version.handler';
import { UploadVersionHandler } from './application/commands/upload-version/upload-version.handler';
import { VersionController } from './api/controllers/version.controller';
import { MobileVersionController } from './api/controllers/mobile-version.controller';
import { HealthController } from './api/controllers/health.controller';
import { DownloadController } from './api/controllers/download.controller';
// Notification imports
import { NotificationMapper } from './infrastructure/persistence/mappers/notification.mapper';
import { NotificationRepositoryImpl } from './infrastructure/persistence/repositories/notification.repository.impl';
import { NOTIFICATION_REPOSITORY } from './domain/repositories/notification.repository';
import { AdminNotificationController, MobileNotificationController } from './api/controllers/notification.controller';
// User Management imports
import { UserQueryRepositoryImpl } from './infrastructure/persistence/repositories/user-query.repository.impl';
import { USER_QUERY_REPOSITORY } from './domain/repositories/user-query.repository';
import { UserController } from './api/controllers/user.controller';
import { UserDetailController } from './api/controllers/user-detail.controller';
import { UserEventConsumerService } from './infrastructure/kafka/user-event-consumer.service';
// CDC Consumer imports
import { CdcConsumerService } from './infrastructure/kafka/cdc-consumer.service';
import { ReferralCdcConsumerService } from './infrastructure/kafka/referral-cdc-consumer.service';
import { WalletCdcConsumerService } from './infrastructure/kafka/wallet-cdc-consumer.service';
import { PlantingCdcConsumerService } from './infrastructure/kafka/planting-cdc-consumer.service';
import { AuthorizationCdcConsumerService } from './infrastructure/kafka/authorization-cdc-consumer.service';
// User Detail Query imports
import { UserDetailQueryRepositoryImpl } from './infrastructure/persistence/repositories/user-detail-query.repository.impl';
import { USER_DETAIL_QUERY_REPOSITORY } from './domain/repositories/user-detail-query.repository';
// System Config imports
import { SystemConfigRepositoryImpl } from './infrastructure/persistence/repositories/system-config.repository.impl';
import { SYSTEM_CONFIG_REPOSITORY } from './domain/repositories/system-config.repository';
import { AdminSystemConfigController, PublicSystemConfigController } from './api/controllers/system-config.controller';
// User Profile System imports (标签、规则、人群包)
import { USER_TAG_REPOSITORY } from './domain/repositories/user-tag.repository';
import { UserTagRepositoryImpl } from './infrastructure/persistence/repositories/user-tag.repository.impl';
import { CLASSIFICATION_RULE_REPOSITORY } from './domain/repositories/classification-rule.repository';
import { ClassificationRuleRepositoryImpl } from './infrastructure/persistence/repositories/classification-rule.repository.impl';
import { AUDIENCE_SEGMENT_REPOSITORY } from './domain/repositories/audience-segment.repository';
import { AudienceSegmentRepositoryImpl } from './infrastructure/persistence/repositories/audience-segment.repository.impl';
import { RuleEngineService } from './application/services/rule-engine.service';
import { UserTaggingService } from './application/services/user-tagging.service';
import { AudienceSegmentService } from './application/services/audience-segment.service';
import { UserTagController } from './api/controllers/user-tag.controller';
import { ClassificationRuleController } from './api/controllers/classification-rule.controller';
import { AudienceSegmentController } from './api/controllers/audience-segment.controller';
import { AutoTagSyncJob } from './infrastructure/jobs/auto-tag-sync.job';
import { ContractBatchDownloadJob } from './infrastructure/jobs/contract-batch-download.job';
// Co-Managed Wallet imports
import { CoManagedWalletController } from './api/controllers/co-managed-wallet.controller';
import { CoManagedWalletService } from './application/services/co-managed-wallet.service';
import { CoManagedWalletMapper } from './infrastructure/persistence/mappers/co-managed-wallet.mapper';
import { CoManagedWalletSessionRepositoryImpl } from './infrastructure/persistence/repositories/co-managed-wallet-session.repository.impl';
import { CoManagedWalletRepositoryImpl } from './infrastructure/persistence/repositories/co-managed-wallet.repository.impl';
import {
  CO_MANAGED_WALLET_SESSION_REPOSITORY,
  CO_MANAGED_WALLET_REPOSITORY,
} from './domain/repositories/co-managed-wallet.repository';
// System Maintenance imports
import { SYSTEM_MAINTENANCE_REPOSITORY } from './domain/repositories/system-maintenance.repository';
import { SystemMaintenanceRepositoryImpl } from './infrastructure/persistence/repositories/system-maintenance.repository.impl';
import { AdminMaintenanceController, MobileMaintenanceController } from './api/controllers/system-maintenance.controller';
import { MaintenanceInterceptor } from './api/interceptors/maintenance.interceptor';
// App Asset imports
import { AdminAppAssetController, PublicAppAssetController } from './api/controllers/app-asset.controller'
// Customer Service Contact imports
import { AdminCustomerServiceContactController, PublicCustomerServiceContactController } from './api/controllers/customer-service-contact.controller';
// [2026-02-05] 新增：合同管理模块
import { ContractController } from './api/controllers/contract.controller';
import { ContractService } from './application/services/contract.service';
// [2026-02-17] 新增：预种计划开关管理
import { PrePlantingConfigController, PublicPrePlantingConfigController } from './pre-planting/pre-planting-config.controller';
import { PrePlantingConfigService } from './pre-planting/pre-planting-config.service';
// [2026-02-27] 新增：预种计划数据代理（admin-service → planting-service 内部 HTTP）
import { PrePlantingProxyService } from './pre-planting/pre-planting-proxy.service';
// [2026-03-02] 新增：推荐链预种统计代理（admin-service → referral-service 内部 HTTP）
import { ReferralProxyService } from './referral/referral-proxy.service';
// [2026-03-02] 纯新增：授权自助申请照片代理（admin-service → authorization-service 内部 HTTP）
import { AuthorizationProxyService } from './authorization/authorization-proxy.service';
import { AuthorizationPhotosController } from './api/controllers/authorization-photos.controller';
// [2026-02-26] 新增：认种树定价配置（总部运营成本压力涨价）
import { AdminTreePricingController, PublicTreePricingController } from './pricing/tree-pricing.controller';
import { TreePricingService } from './pricing/tree-pricing.service';
import { AutoPriceIncreaseJob } from './infrastructure/jobs/auto-price-increase.job';

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      load: configurations,
    }),
    // Serve static files from uploads directory
    ServeStaticModule.forRoot({
      rootPath: join(process.cwd(), process.env.UPLOAD_DIR || 'uploads'),
      serveRoot: '/uploads',
    }),
    // Schedule module for cron jobs
    ScheduleModule.forRoot(),
  ],
  controllers: [
    VersionController,
    MobileVersionController,
    HealthController,
    DownloadController,
    AdminNotificationController,
    MobileNotificationController,
    UserController,
    UserDetailController,
    AdminSystemConfigController,
    PublicSystemConfigController,
    // User Profile System Controllers
    UserTagController,
    ClassificationRuleController,
    AudienceSegmentController,
    // Co-Managed Wallet Controller
    CoManagedWalletController,
    // System Maintenance Controllers
    AdminMaintenanceController,
    MobileMaintenanceController,
    // App Asset Controllers
    AdminAppAssetController,
    PublicAppAssetController,
    // Customer Service Contact Controllers
    AdminCustomerServiceContactController,
    PublicCustomerServiceContactController,
    // [2026-02-05] 新增：合同管理控制器
    ContractController,
    // [2026-02-17] 新增：预种计划开关管理
    PrePlantingConfigController,
    PublicPrePlantingConfigController,
    // [2026-02-26] 新增：认种树定价配置（总部运营成本压力涨价）
    AdminTreePricingController,
    PublicTreePricingController,
    // [2026-03-02] 纯新增：自助申请照片管理
    AuthorizationPhotosController,
  ],
  providers: [
    PrismaService,
    AppVersionMapper,
    FileStorageService,
    PackageParserService,
    {
      provide: APP_VERSION_REPOSITORY,
      useClass: AppVersionRepositoryImpl,
    },
    // Query Handlers
    CheckUpdateHandler,
    ListVersionsHandler,
    GetVersionHandler,
    // Command Handlers
    CreateVersionHandler,
    UpdateVersionHandler,
    DeleteVersionHandler,
    ToggleVersionHandler,
    UploadVersionHandler,
    // Notification
    NotificationMapper,
    {
      provide: NOTIFICATION_REPOSITORY,
      useClass: NotificationRepositoryImpl,
    },
    // User Management
    {
      provide: USER_QUERY_REPOSITORY,
      useClass: UserQueryRepositoryImpl,
    },
    {
      provide: USER_DETAIL_QUERY_REPOSITORY,
      useClass: UserDetailQueryRepositoryImpl,
    },
    UserEventConsumerService,
    // CDC Consumers (Debezium)
    CdcConsumerService,
    ReferralCdcConsumerService,
    WalletCdcConsumerService,
    PlantingCdcConsumerService,
    AuthorizationCdcConsumerService,
    // System Config
    {
      provide: SYSTEM_CONFIG_REPOSITORY,
      useClass: SystemConfigRepositoryImpl,
    },
    // User Profile System (标签、规则、人群包)
    {
      provide: USER_TAG_REPOSITORY,
      useClass: UserTagRepositoryImpl,
    },
    {
      provide: CLASSIFICATION_RULE_REPOSITORY,
      useClass: ClassificationRuleRepositoryImpl,
    },
    {
      provide: AUDIENCE_SEGMENT_REPOSITORY,
      useClass: AudienceSegmentRepositoryImpl,
    },
    RuleEngineService,
    UserTaggingService,
    AudienceSegmentService,
    // Scheduled Jobs
    AutoTagSyncJob,
    ContractBatchDownloadJob,
    // Co-Managed Wallet
    CoManagedWalletMapper,
    CoManagedWalletService,
    {
      provide: CO_MANAGED_WALLET_SESSION_REPOSITORY,
      useClass: CoManagedWalletSessionRepositoryImpl,
    },
    {
      provide: CO_MANAGED_WALLET_REPOSITORY,
      useClass: CoManagedWalletRepositoryImpl,
    },
    // System Maintenance
    {
      provide: SYSTEM_MAINTENANCE_REPOSITORY,
      useClass: SystemMaintenanceRepositoryImpl,
    },
    // Global Interceptors
    {
      provide: APP_INTERCEPTOR,
      useClass: MaintenanceInterceptor,
    },
    // [2026-02-05] 新增：合同管理服务
    ContractService,
    // [2026-02-17] 新增：预种计划开关管理
    PrePlantingConfigService,
    // [2026-02-27] 新增：预种计划数据代理
    PrePlantingProxyService,
    // [2026-03-02] 新增：推荐链预种统计代理
    ReferralProxyService,
    // [2026-03-02] 纯新增：授权自助申请照片代理
    AuthorizationProxyService,
    // [2026-02-26] 新增：认种树定价配置（总部运营成本压力涨价）
    TreePricingService,
    AutoPriceIncreaseJob,
  ],
})
export class AppModule {}
