import { forwardRef, Module } from '@nestjs/common';
import { CqrsModule } from '@nestjs/cqrs';
import { AuthModule } from '@modules/auth';
import { ListingsModule } from '@modules/listings';
import { AI_CONFIG_PROVIDER } from '@modules/shared';
import { SubscriptionsModule } from '@modules/subscriptions';
import { AdjustSubscriptionHandler } from './application/commands/adjust-subscription/adjust-subscription.handler';
import { ApproveKycHandler } from './application/commands/approve-kyc/approve-kyc.handler';
import { ApproveListingHandler } from './application/commands/approve-listing/approve-listing.handler';
import { BanUserHandler } from './application/commands/ban-user/ban-user.handler';
import { BulkModerateListingsHandler } from './application/commands/bulk-moderate-listings/bulk-moderate-listings.handler';
import { ProvisionDeveloperHandler } from './application/commands/provision-developer/provision-developer.handler';
import { ProvisionParkOperatorHandler } from './application/commands/provision-park-operator/provision-park-operator.handler';
import { RejectKycHandler } from './application/commands/reject-kyc/reject-kyc.handler';
import { RejectListingHandler } from './application/commands/reject-listing/reject-listing.handler';
import { UpdateAiSettingsHandler } from './application/commands/update-ai-settings/update-ai-settings.handler';
import { UpdateUserStatusHandler } from './application/commands/update-user-status/update-user-status.handler';
import { AdminAuditListener } from './application/listeners/admin-audit.listener';
import { ModerationAuditListener } from './application/listeners/moderation-audit.listener';
import { UserBannedListener } from './application/listeners/user-banned.listener';
import { UserDeactivatedListener } from './application/listeners/user-deactivated.listener';
import { GetAiSettingsHandler } from './application/queries/get-ai-settings/get-ai-settings.handler';
import { GetAuditLogsHandler } from './application/queries/get-audit-logs/get-audit-logs.handler';
import { GetDashboardStatsHandler } from './application/queries/get-dashboard-stats/get-dashboard-stats.handler';
import { GetFlaggedListingsHandler } from './application/queries/get-flagged-listings/get-flagged-listings.handler';
import { GetKycQueueHandler } from './application/queries/get-kyc-queue/get-kyc-queue.handler';
import { GetModerationAuditLogsHandler } from './application/queries/get-moderation-audit-logs/get-moderation-audit-logs.handler';
import { GetModerationQueueHandler } from './application/queries/get-moderation-queue/get-moderation-queue.handler';
import { GetRevenueStatsHandler } from './application/queries/get-revenue-stats/get-revenue-stats.handler';
import { GetUserDetailHandler } from './application/queries/get-user-detail/get-user-detail.handler';
import { GetUsersHandler } from './application/queries/get-users/get-users.handler';
import { SystemSettingsService } from './application/services/system-settings.service';
import { ADMIN_QUERY_REPOSITORY } from './domain/repositories/admin-query.repository';
import { AUDIT_LOG_REPOSITORY } from './domain/repositories/audit-log.repository';
import { MODERATION_AUDIT_LOG_REPOSITORY } from './domain/repositories/moderation-audit-log.repository';
import { PrismaAdminQueryRepository } from './infrastructure/repositories/prisma-admin-query.repository';
import { PrismaAuditLogRepository } from './infrastructure/repositories/prisma-audit-log.repository';
import { PrismaModerationAuditLogRepository } from './infrastructure/repositories/prisma-moderation-audit-log.repository';
import { SystemSettingsAiConfigProvider } from './infrastructure/adapters/system-settings-ai-config.provider';
import { AdminModerationAuditController } from './presentation/controllers/admin-moderation-audit.controller';
import { AdminModerationController } from './presentation/controllers/admin-moderation.controller';
import { AdminController } from './presentation/controllers/admin.controller';

const CommandHandlers = [
  ApproveListingHandler,
  RejectListingHandler,
  BanUserHandler,
  AdjustSubscriptionHandler,
  UpdateUserStatusHandler,
  ApproveKycHandler,
  RejectKycHandler,
  BulkModerateListingsHandler,
  UpdateAiSettingsHandler,
  ProvisionDeveloperHandler,
  ProvisionParkOperatorHandler,
];

const QueryHandlers = [
  GetModerationQueueHandler,
  GetFlaggedListingsHandler,
  GetDashboardStatsHandler,
  GetRevenueStatsHandler,
  GetUsersHandler,
  GetUserDetailHandler,
  GetKycQueueHandler,
  GetAuditLogsHandler,
  GetModerationAuditLogsHandler,
  GetAiSettingsHandler,
];

@Module({
  imports: [CqrsModule, AuthModule, forwardRef(() => ListingsModule), SubscriptionsModule],
  controllers: [
    AdminController,
    AdminModerationController,
    AdminModerationAuditController,
  ],
  providers: [
    // Repositories
    { provide: ADMIN_QUERY_REPOSITORY, useClass: PrismaAdminQueryRepository },
    { provide: AUDIT_LOG_REPOSITORY, useClass: PrismaAuditLogRepository },
    {
      provide: MODERATION_AUDIT_LOG_REPOSITORY,
      useClass: PrismaModerationAuditLogRepository,
    },

    // Services
    SystemSettingsService,
    { provide: AI_CONFIG_PROVIDER, useClass: SystemSettingsAiConfigProvider },

    // CQRS
    ...CommandHandlers,
    ...QueryHandlers,

    // Event Listeners
    UserBannedListener,
    UserDeactivatedListener,
    AdminAuditListener,
    ModerationAuditListener,
  ],
  exports: [SystemSettingsService, AI_CONFIG_PROVIDER],
})
export class AdminModule {}
