import { HttpModule } from '@nestjs/axios';
import { Module } from '@nestjs/common';
import { APP_INTERCEPTOR } from '@nestjs/core';
import { EventEmitterModule } from '@nestjs/event-emitter';
import { JwtService } from '@nestjs/jwt';
import { TypeOrmModule } from '@nestjs/typeorm';
import { TransformInterceptor } from 'src/interceptors/transform.interceptor';
import { AuthController } from './auth.controller';
import { ResetPassListener } from './listeners/resetpass-listener';
import { SignupListener } from './listeners/signup-listener';
import { AuthService } from './services/auth.service';
import { FacebookService } from './services/facebook.service';
import { GoogleService } from './services/google.service';
import { ResetPasswordService } from './services/reset-password.service';
import { AppleService } from './services/apple.service';
import { UserEntity } from 'src/entities/User.entity';

/**
 * AuthModule - Authentication module for the application
 *
 * This module organizes all authentication-related components including:
 * - Controllers: HTTP endpoints for authentication operations
 * - Services: Business logic for authentication flows
 * - Listeners: Event handlers for authentication-related events
 * - Interceptors: Response transformation
 * - Dependencies: Database, HTTP, JWT, and event infrastructure
 *
 * Features:
 * - Email/password authentication
 * - Social login (Facebook, Google, Apple)
 * - Password reset functionality
 * - Event-driven architecture for side effects
 * - Response standardization
 *
 * @example
 * // Import into main AppModule
 * @Module({
 *   imports: [AuthModule, ...],
 * })
 * export class AppModule {}
 */
@Module({
  imports: [
    // Database integration: provides UserEntity repository
    TypeOrmModule.forFeature([UserEntity]),

    // HTTP client configuration for external API calls (social login)
    HttpModule.register({
      timeout: 5000, // 5-second timeout for external requests
      maxRedirects: 5, // Maximum redirects for HTTP requests
    }),

    // Event system for decoupled side effects (emails, notifications, etc.)
    EventEmitterModule.forRoot(),
  ],

  // Authentication HTTP endpoints
  controllers: [AuthController],

  // Authentication providers and services
  providers: [
    // Core JWT service for token generation/verification
    JwtService,

    // Main authentication service (email/password login)
    AuthService,

    // Password reset service
    ResetPasswordService,

    // Social authentication services
    FacebookService,
    GoogleService,
    AppleService,

    // Event listeners for side effects
    SignupListener, // Handles post-signup events (welcome emails, etc.)
    ResetPassListener, // Handles password reset events

    // Global response transformation interceptor
    // Standardizes all API responses across the application
    {
      provide: APP_INTERCEPTOR,
      useClass: TransformInterceptor,
    },
  ],

  // Optional: Export services for use in other modules
  // exports: [AuthService, JwtService],
})
export class AuthModule {}
