/**
 * AgentToolsCollector — 全局单例,收集所有 forFeature 登记的业务 service class。
 *
 * 设计来源 (#410 plan-review Q1.1 = C):
 *   装饰器 + forFeature 显式收集 + NestJS DI 标准模式
 *   (不用 DiscoveryService 全局扫,因为 forFeature 边界更可控)
 *
 * 单例模式 + Set 去重保证多次同 service forFeature 不重复注册。
 * @nestjs/typeorm 等成熟 forFeature 模块也是这种全局收集模式。
 *
 * v1.0 PR-A 仅承载 service class 清单;实际"扫描 @AgentTool 方法 + 注册到 ToolRegistry"
 * 由 AgentToolBootstrap (同目录) 在 onApplicationBootstrap 完成。
 */

import { Injectable, Type } from '@nestjs/common';
import { createLogger } from '@core/observability/logging/config/winston.config';

const logger = createLogger('AgentToolsCollector');

@Injectable()
export class AgentToolsCollector {
  private readonly services = new Set<Type>();

  /**
   * 由 AgentToolsModule.forFeature 的 useFactory 在 NestJS DI 解析时调用,
   * 把业务 module 登记的 service class 加入全局清单。
   */
  register(svcs: readonly Type[]): void {
    let added = 0;
    for (const s of svcs) {
      if (!this.services.has(s)) {
        this.services.add(s);
        added += 1;
      }
    }
    if (added > 0) {
      logger.log(`registered ${added} service class(es); total = ${this.services.size}`);
    }
  }

  /** 启动期扫描时返回所有登记的 service class */
  list(): Type[] {
    return [...this.services];
  }
}
