import { Injectable, NotFoundException } from '@nestjs/common';
import type { PullStrategy, SyncContext } from './adapter.interface';
import { MockPullStrategy } from './mock-pull.adapter';

/**
 * PullStrategyRegistry — Pull 适配器注册中心(first match wins)。
 *
 * 已注册:
 *   - MockPullStrategy(host.pullConfig.adapter === 'mock')— 假宿主,测试用
 *
 * 真实 strategy(等首个宿主接入时实装):
 *   - ApiKeyPullStrategy   Bearer token 鉴权
 *   - HttpLoginPullStrategy  Session cookie 鉴权
 *   - OAuth2PullStrategy   OAuth2 / OIDC(W6+)
 */
@Injectable()
export class PullStrategyRegistry {
  private readonly strategies: PullStrategy[];

  constructor(mock: MockPullStrategy) {
    this.strategies = [mock];
  }

  resolve(ctx: SyncContext): PullStrategy {
    const found = this.strategies.find((s) => s.appliesTo(ctx));
    if (!found) {
      throw new NotFoundException(
        `No pull strategy applies to host=${ctx.hostId};pullConfig.adapter 不识别,数据请通过 cold-import 入站`,
      );
    }
    return found;
  }
}
