/**
 * 管理员业务模块
 *
 * 功能描述：
 * - 提供后台管理的HTTP API（管理员登录、用户管理、密码重置等）
 * - 集成管理员核心服务和日志管理服务
 * - 导出管理员服务供其他模块使用
 *
 * 职责分离：
 * - 模块依赖管理和服务注册
 * - HTTP层与业务流程编排
 * - 核心鉴权与密码策略由AdminCoreService提供
 *
 * 最近修改：
 * - 2026-01-08: 注释规范优化 - 修正import路径，创建缺失的控制器和服务文件 (修改者: moyin)
 * - 2026-01-07: 代码规范优化 - 修正文件命名规范，更新作者信息和修改记录
 *
 * @author moyin
 * @version 1.0.2
 * @since 2025-12-19
 * @lastModified 2026-01-08
 */

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AdminCoreModule } from '../../core/admin_core/admin_core.module';
import { LoggerModule } from '../../core/utils/logger/logger.module';
import { UsersModule } from '../../core/db/users/users.module';
import { UserProfilesModule } from '../../core/db/user_profiles/user_profiles.module';
import { SessionCoreModule } from '../../core/session_core/session_core.module';
import { AdminController } from './admin.controller';
import { AdminService } from './admin.service';
import { AdminDatabaseController } from './admin_database.controller';
import { AdminOperationLogController } from './admin_operation_log.controller';
import { DatabaseManagementService } from './database_management.service';
import { AdminOperationLogService } from './admin_operation_log.service';
import { AdminOperationLogMemoryService } from './admin_operation_log_memory.service';
import { AdminOperationLog } from './admin_operation_log.entity';
import { AdminDatabaseExceptionFilter } from './admin_database_exception.filter';
import { AdminOperationLogInterceptor } from './admin_operation_log.interceptor';
import { TestLabController } from './test_lab.controller';
import { TestLabService } from './test_lab.service';
import { TestLabGuard } from './test_lab.guard';
import { ChatGatewayModule } from '../../gateway/chat/chat.gateway.module';
import { AuthModule } from '../auth/auth.module';
import { LoginCoreModule } from '../../core/login_core/login_core.module';

/**
 * 检查数据库配置是否完整
 *
 * @returns 是否配置了数据库
 */
function isDatabaseConfigured(): boolean {
  const requiredEnvVars = ['DB_HOST', 'DB_PORT', 'DB_USERNAME', 'DB_PASSWORD', 'DB_NAME'];
  return requiredEnvVars.every(varName => process.env[varName]);
}

@Module({
  imports: [
    AdminCoreModule,
    LoggerModule,
    UsersModule,
    SessionCoreModule,
    AuthModule,
    LoginCoreModule,
    ChatGatewayModule,
    UserProfilesModule,
    // 注意：ZulipAccountsModule 是全局模块，已在 AppModule 中导入，无需重复导入
    // 注册AdminOperationLog实体
    ...(isDatabaseConfigured() ? [TypeOrmModule.forFeature([AdminOperationLog])] : [])
  ],
  controllers: [
    AdminController,
    AdminDatabaseController,
    AdminOperationLogController,
    TestLabController,
  ],
  providers: [
    AdminService,
    DatabaseManagementService,
    {
      provide: AdminOperationLogService,
      useClass: isDatabaseConfigured()
        ? AdminOperationLogService
        : AdminOperationLogMemoryService,
    },
    AdminDatabaseExceptionFilter,
    AdminOperationLogInterceptor,
    TestLabService,
    TestLabGuard,
  ],
  exports: [
    AdminService,
    DatabaseManagementService,
    AdminOperationLogService,
    TestLabService,
  ], // 导出服务供其他模块使用
})
export class AdminModule {}
