import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ScheduleModule } from '@nestjs/schedule';
import { DatabaseModule } from '@it0/database';

// Domain Entities
import { Plan } from './domain/entities/plan.entity';
import { Subscription } from './domain/entities/subscription.entity';
import { Invoice } from './domain/entities/invoice.entity';
import { InvoiceItem } from './domain/entities/invoice-item.entity';
import { Payment } from './domain/entities/payment.entity';
import { PaymentMethod } from './domain/entities/payment-method.entity';
import { UsageAggregate } from './domain/entities/usage-aggregate.entity';

// Domain Services
import { OverageCalculatorService } from './domain/services/overage-calculator.service';
import { SubscriptionLifecycleService } from './domain/services/subscription-lifecycle.service';
import { InvoiceGeneratorService } from './domain/services/invoice-generator.service';

// Infrastructure Repositories
import { PlanRepository } from './infrastructure/repositories/plan.repository';
import { SubscriptionRepository } from './infrastructure/repositories/subscription.repository';
import { InvoiceRepository } from './infrastructure/repositories/invoice.repository';
import { PaymentRepository } from './infrastructure/repositories/payment.repository';
import { UsageAggregateRepository } from './infrastructure/repositories/usage-aggregate.repository';

// Payment Providers
import { PaymentProviderRegistry } from './infrastructure/payment-providers/payment-provider.registry';
import { StripeProvider } from './infrastructure/payment-providers/stripe/stripe.provider';
import { AlipayProvider } from './infrastructure/payment-providers/alipay/alipay.provider';
import { WeChatPayProvider } from './infrastructure/payment-providers/wechat/wechat-pay.provider';
import { CryptoProvider } from './infrastructure/payment-providers/crypto/crypto.provider';

// Application Use Cases
import { CreateSubscriptionUseCase } from './application/use-cases/create-subscription.use-case';
import { ChangePlanUseCase } from './application/use-cases/change-plan.use-case';
import { CancelSubscriptionUseCase } from './application/use-cases/cancel-subscription.use-case';
import { GenerateMonthlyInvoiceUseCase } from './application/use-cases/generate-monthly-invoice.use-case';
import { AggregateUsageUseCase } from './application/use-cases/aggregate-usage.use-case';
import { CheckTokenQuotaUseCase } from './application/use-cases/check-token-quota.use-case';
import { CreatePaymentSessionUseCase } from './application/use-cases/create-payment-session.use-case';
import { HandlePaymentWebhookUseCase } from './application/use-cases/handle-payment-webhook.use-case';

// Controllers
import { PlanController } from './interfaces/rest/controllers/plan.controller';
import { SubscriptionController } from './interfaces/rest/controllers/subscription.controller';
import { InvoiceController } from './interfaces/rest/controllers/invoice.controller';
import { PaymentMethodController } from './interfaces/rest/controllers/payment-method.controller';
import { WebhookController } from './interfaces/rest/controllers/webhook.controller';

// Cron scheduler
import { BillingScheduler } from './infrastructure/scheduler/billing.scheduler';
import { PlanSeedService } from './infrastructure/seed/plan-seed.service';

@Module({
  imports: [
    ConfigModule.forRoot({ isGlobal: true }),
    DatabaseModule.forRoot(),
    TypeOrmModule.forFeature([
      Plan, Subscription, Invoice, InvoiceItem,
      Payment, PaymentMethod, UsageAggregate,
    ]),
    ScheduleModule.forRoot(),
  ],
  controllers: [
    PlanController,
    SubscriptionController,
    InvoiceController,
    PaymentMethodController,
    WebhookController,
  ],
  providers: [
    // Domain services
    OverageCalculatorService,
    SubscriptionLifecycleService,
    InvoiceGeneratorService,

    // Repositories
    PlanRepository,
    SubscriptionRepository,
    InvoiceRepository,
    PaymentRepository,
    UsageAggregateRepository,

    // Payment providers
    StripeProvider,
    AlipayProvider,
    WeChatPayProvider,
    CryptoProvider,
    PaymentProviderRegistry,

    // Use cases
    CreateSubscriptionUseCase,
    ChangePlanUseCase,
    CancelSubscriptionUseCase,
    GenerateMonthlyInvoiceUseCase,
    AggregateUsageUseCase,
    CheckTokenQuotaUseCase,
    CreatePaymentSessionUseCase,
    HandlePaymentWebhookUseCase,

    // Scheduler
    BillingScheduler,
    PlanSeedService,
  ],
})
export class BillingModule {}
