import { Global, Module } from '@nestjs/common';
import {
  GlobalPublicController,
  GlobalSecureController,
} from './global.controller';
import { TypeOrmModule } from '@nestjs/typeorm';
import { GlobalService } from './global.service';
import { CacheModule } from '@nestjs/cache-manager';
import { LmsTableSettingsEntity } from 'src/entities/LmsTableSettings.entity';
import { MenuGroupsEntity } from 'src/entities/MenuGroups.entity';
import { TimezoneMastersEntity } from 'src/entities/TimezoneMasters.entity';
import { UserEntity } from 'src/entities/User.entity';
import { FavoriteMenuEntity } from 'src/entities/FavoriteMenu.entity';
import { JwtModule } from '@nestjs/jwt';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { UserAuthorizationsEntity } from 'src/entities/UserAuthorizations.entity';
import { RecentVisitedMenusEntity } from 'src/entities/RecentVisitedMenus.entity';
import { UsersEntity } from 'src/entities/Users.entity';

/**
 * Global Module
 *
 * This module encapsulates all global/user settings functionality for the application.
 * It provides endpoints for both public (unauthenticated) and secure (authenticated)
 * operations related to user preferences, settings, and session management.
 *
 * The module is organized into:
 * - Two controllers (public and secure) for different access levels
 * - A shared service for business logic
 * - Multiple TypeORM entities for data persistence
 * - Cache integration for performance optimization
 *
 * @Module - Decorator that defines this as a NestJS module with its dependencies
 *
 * @example
 * // Import in AppModule:
 * imports: [GlobalModule, ...]
 *
 * @example
 * // Use in other modules:
 * imports: [GlobalModule] // To access exported providers
 */
@Global()
@Module({
  /**
   * Controllers that handle HTTP requests for this module.
   * Organized into two separate controllers for clarity:
   *
   * 1. GlobalPublicController - Handles endpoints that don't require authentication
   *    - Session validation
   *
   * 2. GlobalSecureController - Handles endpoints that require valid session
   *    - Table settings management
   *    - Menu access control
   *    - Timezone management
   *    - Menu bookmarks
   *    - Recent visits tracking
   */
  controllers: [GlobalPublicController, GlobalSecureController],

  /**
   * Providers (services, repositories, helpers) for this module.
   * GlobalService contains the main business logic and is injected into both controllers.
   *
   * @note Consider splitting GlobalService into smaller, more focused services:
   * - TableSettingsService
   * - MenuService
   * - TimezoneService
   * - BookmarkService
   */
  providers: [GlobalService],

  /**
   * Modules and entities that this module depends on.
   *
   * 1. CacheModule - For caching frequently accessed data (table settings, etc.)
   * 2. TypeOrmModule.forFeature() - Registers entities for dependency injection
   * 3. JwtModule - (Recommended addition) For session/authentication handling
   */
  imports: [
    // Cache module for storing temporary data (Redis/memory)
    CacheModule.register(),

    // Register TypeORM entities that this module uses
    TypeOrmModule.forFeature([
      LmsTableSettingsEntity, // User table settings
      MenuGroupsEntity, // Menu structure and permissions
      TimezoneMastersEntity, // Timezone data
      UserEntity, // User information (for relationships)
      FavoriteMenuEntity, // User menu bookmarks
      UserAuthorizationsEntity,
      RecentVisitedMenusEntity,
      UsersEntity,
    ]),

    // RECOMMENDED: Add JWT module for authentication if not already in AppModule
    // JwtModule.registerAsync({
    //   imports: [ConfigModule],
    //   inject: [ConfigService],
    //   useFactory: (configService: ConfigService) => ({
    //     secret: configService.get<string>('JWT_SECRET'),
    //     signOptions: { expiresIn: configService.get<string>('JWT_EXPIRES_IN') },
    //   }),
    // }),
  ],

  /**
   * Providers that should be available to other modules importing this module.
   *
   * 1. GlobalService - Makes the service available for other modules
   * 2. TypeOrmModule - Makes entities available for other modules
   *
   * @note Be careful when exporting TypeOrmModule as it exposes all registered
   * entities. Consider creating a separate shared module for common entities.
   */
  exports: [GlobalService, TypeOrmModule],
})
export class GlobalModule {}
