/**
 * Zulip集成业务模块测试
 * 
 * 功能描述：
 * - 测试模块配置的正确性
 * - 验证依赖注入配置的完整性
 * - 测试服务的注册
 * - 验证模块导出的正确性
 * 
 * 测试范围：
 * - 模块导入配置验证
 * - 服务提供者注册验证
 * - 模块导出验证
 * 
 * 架构说明：
 * - Business层：仅包含业务逻辑服务
 * - Controller已迁移到Gateway层（src/gateway/zulip/）
 * 
 * 更新记录：
 * - 2026-01-14: 架构优化 - Controller迁移到Gateway层，更新测试用例 (修改者: moyin)
 * - 2026-01-14: 重构后更新 - 聊天功能已迁移到 gateway/chat 和 business/chat 模块
 *   本模块仅保留 Zulip 账号管理和事件处理功能
 * 
 * @author moyin
 * @version 3.0.0
 * @since 2026-01-12
 * @lastModified 2026-01-14
 */

import { ZulipModule } from './zulip.module';
import { ZulipEventProcessorService } from './services/zulip_event_processor.service';
import { ZulipAccountsBusinessService } from './services/zulip_accounts_business.service';
import { DynamicConfigManagerService } from '../../core/zulip_core/services/dynamic_config_manager.service';

describe('ZulipModule', () => {
  describe('Module Configuration', () => {
    it('should be defined', () => {
      expect(ZulipModule).toBeDefined();
    });

    it('should have correct module metadata', () => {
      const moduleMetadata = Reflect.getMetadata('imports', ZulipModule) || [];
      const providersMetadata = Reflect.getMetadata('providers', ZulipModule) || [];
      const controllersMetadata = Reflect.getMetadata('controllers', ZulipModule) || [];
      const exportsMetadata = Reflect.getMetadata('exports', ZulipModule) || [];

      // 验证导入的模块数量
      expect(moduleMetadata.length).toBeGreaterThanOrEqual(6);
      
      // 验证提供者数量（2个业务服务）
      expect(providersMetadata).toHaveLength(2);
      
      // 验证控制器数量（Controller已迁移到Gateway层，应为0）
      expect(controllersMetadata).toHaveLength(0);
      
      // 验证导出数量（3个服务：2个业务服务 + 1个重新导出的Core服务）
      expect(exportsMetadata).toHaveLength(3);
    });
  });

  describe('Service Providers', () => {
    it('should include ZulipEventProcessorService in providers', () => {
      const providers = Reflect.getMetadata('providers', ZulipModule) || [];
      expect(providers).toContain(ZulipEventProcessorService);
    });

    it('should include ZulipAccountsBusinessService in providers', () => {
      const providers = Reflect.getMetadata('providers', ZulipModule) || [];
      expect(providers).toContain(ZulipAccountsBusinessService);
    });

    it('should NOT include DynamicConfigManagerService in providers (provided by ZulipCoreModule)', () => {
      const providers = Reflect.getMetadata('providers', ZulipModule) || [];
      // DynamicConfigManagerService 由 ZulipCoreModule 提供，不在本模块的 providers 中
      expect(providers).not.toContain(DynamicConfigManagerService);
    });
  });

  describe('Controllers (Migrated to Gateway Layer)', () => {
    it('should NOT have any controllers (migrated to src/gateway/zulip/)', () => {
      const controllers = Reflect.getMetadata('controllers', ZulipModule) || [];
      // 所有Controller已迁移到Gateway层
      expect(controllers).toHaveLength(0);
    });
  });

  describe('Module Structure', () => {
    it('should have proper module architecture', () => {
      // 验证模块结构的合理性
      const moduleClass = ZulipModule;
      expect(moduleClass).toBeDefined();
      expect(typeof moduleClass).toBe('function');
    });

    it('should follow NestJS module conventions', () => {
      // 验证模块遵循NestJS约定
      const moduleMetadata = Reflect.getMetadata('imports', ZulipModule) ||
                           Reflect.getMetadata('providers', ZulipModule) ||
                           Reflect.getMetadata('controllers', ZulipModule) ||
                           Reflect.getMetadata('exports', ZulipModule);
      expect(moduleMetadata).toBeDefined();
    });
  });

  describe('Dependency Integration', () => {
    it('should integrate with core modules correctly', () => {
      // 验证与核心模块的集成
      const imports = Reflect.getMetadata('imports', ZulipModule) || [];
      expect(imports.length).toBeGreaterThan(0);
    });

    it('should have proper service dependencies', () => {
      // 验证服务依赖关系
      const providers = Reflect.getMetadata('providers', ZulipModule) || [];
      expect(providers).toContain(ZulipEventProcessorService);
      expect(providers).toContain(ZulipAccountsBusinessService);
    });

    it('should export essential services', () => {
      // 验证导出的服务
      const exports = Reflect.getMetadata('exports', ZulipModule) || [];
      expect(exports).toContain(ZulipEventProcessorService);
      expect(exports).toContain(ZulipAccountsBusinessService);
      expect(exports).toContain(DynamicConfigManagerService);
    });
  });

  describe('Module Instantiation', () => {
    it('should create module instance without errors', () => {
      expect(() => new ZulipModule()).not.toThrow();
    });

    it('should be a valid NestJS module', () => {
      const instance = new ZulipModule();
      expect(instance).toBeInstanceOf(ZulipModule);
    });
  });

  describe('Configuration Validation', () => {
    it('should have all required imports', () => {
      const imports = Reflect.getMetadata('imports', ZulipModule) || [];
      
      // 验证必需的模块导入（至少6个）
      expect(imports.length).toBeGreaterThanOrEqual(6);
    });

    it('should have all required providers', () => {
      const providers = Reflect.getMetadata('providers', ZulipModule) || [];
      
      // 验证所有必需的服务提供者
      const requiredProviders = [
        ZulipEventProcessorService,
        ZulipAccountsBusinessService,
      ];
      
      requiredProviders.forEach(provider => {
        expect(providers).toContain(provider);
      });
    });

    it('should have no controllers (Business layer)', () => {
      const controllers = Reflect.getMetadata('controllers', ZulipModule) || [];
      
      // Business层不应该包含Controller
      expect(controllers).toHaveLength(0);
    });
  });

  describe('Module Metadata Validation', () => {
    it('should have correct imports configuration', () => {
      const imports = Reflect.getMetadata('imports', ZulipModule) || [];
      
      // 验证导入模块的数量和类型
      expect(Array.isArray(imports)).toBe(true);
      expect(imports.length).toBeGreaterThanOrEqual(6);
    });

    it('should have correct providers configuration', () => {
      const providers = Reflect.getMetadata('providers', ZulipModule) || [];
      
      // 验证提供者的数量和类型（2个业务服务）
      expect(Array.isArray(providers)).toBe(true);
      expect(providers).toHaveLength(2);
    });

    it('should have correct controllers configuration (empty for Business layer)', () => {
      const controllers = Reflect.getMetadata('controllers', ZulipModule) || [];
      
      // Business层不包含Controller
      expect(Array.isArray(controllers)).toBe(true);
      expect(controllers).toHaveLength(0);
    });

    it('should have correct exports configuration', () => {
      const exports = Reflect.getMetadata('exports', ZulipModule) || [];
      
      // 验证导出的数量和类型（3个服务）
      expect(Array.isArray(exports)).toBe(true);
      expect(exports).toHaveLength(3);
    });
  });

  describe('Architecture Compliance', () => {
    it('should not include chat-related services (migrated to business/chat)', () => {
      const providers = Reflect.getMetadata('providers', ZulipModule) || [];
      const providerNames = providers.map((p: any) => p.name || p.toString());
      
      // 验证聊天相关服务已迁移
      expect(providerNames).not.toContain('ZulipService');
      expect(providerNames).not.toContain('SessionManagerService');
      expect(providerNames).not.toContain('MessageFilterService');
      expect(providerNames).not.toContain('SessionCleanupService');
      expect(providerNames).not.toContain('CleanWebSocketGateway');
    });

    it('should not include any controllers (migrated to gateway/zulip)', () => {
      const controllers = Reflect.getMetadata('controllers', ZulipModule) || [];
      
      // 验证所有Controller已迁移到Gateway层
      expect(controllers).toHaveLength(0);
    });

    it('should follow four-layer architecture (Business layer has no controllers)', () => {
      const controllers = Reflect.getMetadata('controllers', ZulipModule) || [];
      const providers = Reflect.getMetadata('providers', ZulipModule) || [];
      
      // Business层规范：只有Service，没有Controller
      expect(controllers).toHaveLength(0);
      expect(providers.length).toBeGreaterThan(0);
      
      // 验证所有provider都是Service类型
      const providerNames = providers.map((p: any) => p.name || '');
      providerNames.forEach((name: string) => {
        expect(name).toMatch(/Service$/);
      });
    });
  });
});
