import { Module } from "@nestjs/common";
import { APP_FILTER, APP_PIPE } from "@nestjs/core";
import { RouterModule } from "@nestjs/core";
import { ConfigModule, ConfigService } from "@nestjs/config";
import { ThrottlerModule } from "@nestjs/throttler";
import { ZodValidationPipe } from "nestjs-zod";
import { ZodValidationExceptionFilter } from "@customer-portal/validation/nestjs";

// Configuration
import { appConfig } from "@bff/core/config/app.config";
import { createThrottlerConfig } from "@bff/core/config/throttler.config";
import { apiRoutes } from "@bff/core/config/router.config";

// Core Modules
import { LoggingModule } from "@bff/core/logging/logging.module";
import { SecurityModule } from "@bff/core/security/security.module";
import { PrismaModule } from "@bff/infra/database/prisma.module";
import { RedisModule } from "@bff/infra/redis/redis.module";
import { CacheModule } from "@bff/infra/cache/cache.module";
import { QueueModule } from "@bff/infra/queue/queue.module";
import { AuditModule } from "@bff/infra/audit/audit.module";
import { EmailModule } from "@bff/infra/email/email.module";

// External Integration Modules
import { IntegrationsModule } from "@bff/integrations/integrations.module";
import { SalesforceEventsModule } from "@bff/integrations/salesforce/events/events.module";

// Feature Modules
import { AuthModule } from "@bff/modules/auth/auth.module";
import { UsersModule } from "@bff/modules/users/users.module";
import { MappingsModule } from "@bff/modules/id-mappings/mappings.module";
import { CatalogModule } from "@bff/modules/catalog/catalog.module";
import { OrdersModule } from "@bff/modules/orders/orders.module";
import { InvoicesModule } from "@bff/modules/invoices/invoices.module";
import { SubscriptionsModule } from "@bff/modules/subscriptions/subscriptions.module";
import { CurrencyModule } from "@bff/modules/currency/currency.module";
import { SupportModule } from "@bff/modules/support/support.module";

// System Modules
import { HealthModule } from "@bff/modules/health/health.module";

/**
 * Main application module
 *
 * Architecture:
 * - Core infrastructure modules provide foundational services
 * - External integration modules handle third-party services
 * - Feature modules implement business logic
 * - System modules provide monitoring and health checks
 *
 * All feature modules are grouped under "/api" prefix via RouterModule
 * Health endpoints remain at root level for monitoring tools
 */
@Module({
  imports: [
    // === CONFIGURATION ===
    ConfigModule.forRoot(appConfig),

    // === INFRASTRUCTURE ===
    LoggingModule,
    SecurityModule,
    ThrottlerModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: createThrottlerConfig,
    }),

    // === CORE SERVICES ===
    PrismaModule,
    RedisModule,
    CacheModule,
    QueueModule,
    AuditModule,
    EmailModule,

    // === EXTERNAL INTEGRATIONS ===
    IntegrationsModule,
    SalesforceEventsModule,

    // === FEATURE MODULES ===
    AuthModule,
    UsersModule,
    MappingsModule,
    CatalogModule,
    OrdersModule,
    InvoicesModule,
    SubscriptionsModule,
    CurrencyModule,
    SupportModule,

    // === SYSTEM MODULES ===
    HealthModule,

    // === ROUTING ===
    RouterModule.register(apiRoutes),
  ],
  providers: [
    {
      provide: APP_PIPE,
      useClass: ZodValidationPipe,
    },
    {
      provide: APP_FILTER,
      useClass: ZodValidationExceptionFilter,
    },
  ],
})
export class AppModule {}
