import { Module } from '@nestjs/common';
import { CqrsModule } from '@nestjs/cqrs';
import { CancelSubscriptionHandler } from './application/commands/cancel-subscription/cancel-subscription.handler';
import { CreateSubscriptionHandler } from './application/commands/create-subscription/create-subscription.handler';
import { MeterUsageHandler } from './application/commands/meter-usage/meter-usage.handler';
import { UpgradeSubscriptionHandler } from './application/commands/upgrade-subscription/upgrade-subscription.handler';
import { CheckQuotaHandler } from './application/queries/check-quota/check-quota.handler';
import { GetBillingHistoryHandler } from './application/queries/get-billing-history/get-billing-history.handler';
import { GetPlanHandler } from './application/queries/get-plan/get-plan.handler';
import { SUBSCRIPTION_REPOSITORY } from './domain/repositories/subscription.repository';
import { BankTransferSubscriptionActivationHandler } from './infrastructure/event-handlers/bank-transfer-subscription-activation.handler';
import { ListingCreatedUsageHandler } from './infrastructure/event-handlers/listing-created-usage.handler';
import { SavedSearchCreatedUsageHandler } from './infrastructure/event-handlers/saved-search-created-usage.handler';
import { PrismaSubscriptionRepository } from './infrastructure/repositories/prisma-subscription.repository';
import { SubscriptionsController } from './presentation/controllers/subscriptions.controller';
import { QuotaGuard } from './presentation/guards/quota.guard';

const CommandHandlers = [
  CreateSubscriptionHandler,
  UpgradeSubscriptionHandler,
  CancelSubscriptionHandler,
  MeterUsageHandler,
];

const QueryHandlers = [
  GetPlanHandler,
  CheckQuotaHandler,
  GetBillingHistoryHandler,
];

@Module({
  imports: [CqrsModule],
  controllers: [SubscriptionsController],
  providers: [
    // Repositories
    { provide: SUBSCRIPTION_REPOSITORY, useClass: PrismaSubscriptionRepository },

    // Guards
    QuotaGuard,

    // Event Listeners
    ListingCreatedUsageHandler,
    SavedSearchCreatedUsageHandler,
    BankTransferSubscriptionActivationHandler,

    // CQRS
    ...CommandHandlers,
    ...QueryHandlers,
  ],
  exports: [SUBSCRIPTION_REPOSITORY, QuotaGuard],
})
export class SubscriptionsModule {}
