// Import core NestJS module decorator
import { Module } from '@nestjs/common';

// Import controller
import { UserController } from './user.controller';

// Import TypeORM integration for database operations
import { TypeOrmModule } from '@nestjs/typeorm';

// Import JWT service for token handling
import { JwtService } from '@nestjs/jwt';

// Import service containing business logic
import { UserService } from './user.service';

// Import multiple entity definitions (9 different entities!)
import { UserEntity } from 'src/entities/User.entity';
import { CacheModule } from '@nestjs/cache-manager';
import { UserBranchEntity } from 'src/entities/UserBranch.entity';
import { DesignationMastersEntity } from 'src/entities/DesignationMasters.entity';
import { DivisionMastersEntity } from 'src/entities/DivisionMasters.entity';
import { UserDivisionsEntity } from 'src/entities/UserDivisions.entity';
import { LmsTableSettingsEntity } from 'src/entities/LmsTableSettings.entity';
import { CountryEntity } from 'src/entities/Country.entity';
import { ThemeEntity } from 'src/entities/Theme.entity';
import { RedisService } from '../redis/redis.service';

/**
 * UserModule - Feature module for comprehensive user management and personalization
 *
 * This module encapsulates all components related to user management, including:
 * - User profile and authentication data
 * - Organizational structure integration (branches, divisions, designations)
 * - User preferences and personalization (themes, table settings)
 * - Session management and caching
 *
 * Business Domain: User Management & Personalization System
 * - Manages user accounts and profiles
 * - Handles user preferences and UI customizations
 * - Integrates with organizational hierarchy
 * - Supports multi-session user experience
 *
 * Module Characteristics:
 * - 9 entity integrations for comprehensive user management
 * - Caching for performance optimization
 * - Redis integration for session management
 * - JWT support for authentication
 */
@Module({
  /**
   * controllers - Array of controllers that handle HTTP requests
   *
   * UserController: Provides user management endpoints
   * - User profile retrieval and updates
   * - Organizational structure access (branches, divisions)
   * - Preference management (themes, menu views, table settings)
   * - Session synchronization for preferences
   */
  controllers: [UserController],

  /**
   * providers - Injectable services available within this module
   *
   * 1. UserService: Core business logic for user operations
   *    - User profile management and retrieval
   *    - Preference management (themes, table settings, menu views)
   *    - Organizational structure queries (branches, divisions)
   *    - Integration with multiple entity repositories
   *
   * 2. JwtService: Utility for JWT token operations
   *    - Used for token generation and validation
   *    - Likely used by authentication guards in other modules
   *    - Supports stateless authentication patterns
   *
   * 3. RedisService: Custom Redis service for session and cache management
   *    - Manages user sessions and session data
   *    - Handles real-time preference updates
   *    - Provides caching layer for user data
   */
  providers: [UserService, JwtService, RedisService],

  /**
   * imports - External modules and dependencies required by this module
   *
   * 1. CacheModule.register(): Configures the caching system
   *    - Provides in-memory caching by default
   *    - Important for frequently accessed user data and preferences
   *    - Reduces database load for user profile queries
   *
   * 2. TypeOrmModule.forFeature([...]): Registers 9 entities with TypeORM
   *    - Creates repositories for each entity
   *    - Enables complex user-related queries
   *    - Supports user preference and organizational structure management
   */
  imports: [
    CacheModule.register(), // Default cache configuration

    /**
     * TypeOrmModule.forFeature with 9 related entities:
     * This represents a comprehensive user management system:
     */
    TypeOrmModule.forFeature([
      /**
       * 1. UserEntity (Core Entity)
       *    - Main user profile and authentication data
       *    - Stores username, email, password hash, status
       *    - Contains personal information and contact details
       *    - Base entity for user management
       */
      UserEntity,

      /**
       * 2. UserBranchEntity (Organizational Structure)
       *    - Maps users to functional branches/business units
       *    - Many-to-many relationship between users and branches
       *    - Used for access control and organizational filtering
       *    - Supports multi-branch user assignments
       */
      UserBranchEntity,

      /**
       * 3. DesignationMastersEntity (Job Roles)
       *    - Reference table for job titles/designations
       *    - Examples: Manager, Developer, Administrator, etc.
       *    - Used for role-based access and reporting structures
       *    - May include hierarchy levels and permissions
       */
      DesignationMastersEntity,

      /**
       * 4. DivisionMastersEntity (Organizational Divisions)
       *    - Reference table for organizational divisions/departments
       *    - Examples: Sales, Marketing, IT, HR, Finance
       *    - Defines organizational structure at division level
       *    - Used for reporting and access control
       */
      DivisionMastersEntity,

      /**
       * 5. UserDivisionsEntity (User-Division Mapping)
       *    - Maps users to organizational divisions
       *    - Many-to-many relationship between users and divisions
       *    - Users can belong to multiple divisions
       *    - Used for organizational access and reporting
       */
      UserDivisionsEntity,

      /**
       * 6. LmsTableSettingsEntity (User Preferences)
       *    - Stores user-specific table/view preferences
       *    - Column visibility, order, sorting, filtering
       *    - Pagination settings and view configurations
       *    - Per-user customization of data tables
       */
      LmsTableSettingsEntity,

      /**
       * 7. CountryEntity (Geographic Reference)
       *    - Country master data for user profiles
       *    - Used for address information and regional settings
       *    - May influence language, currency, timezone preferences
       *    - Reference data for dropdowns and validations
       */
      CountryEntity,

      /**
       * 8. ThemeEntity (UI Customization)
       *    - Theme definitions and configurations
       *    - Color schemes, layouts, styling options
       *    - User-selectable UI themes
       *    - May include dark/light mode, accent colors, etc.
       */
      ThemeEntity,
    ]),
  ],
})
export class UserModule {}
