import { Injectable } from '@nestjs/common';
import { PersonaFeatureKey, acquisitionChannelLabelZh } from '@pac/types';
import type {
  FeatureExtractor,
  FeatureExtractorContext,
  PersonaFeatureDraft,
} from './feature.interface';

/**
 * acquisition_channel 获客渠道(初诊来源,A.2.1)— 规则层,snapshot
 *
 * 口径(图):客户首次到诊的获客来源,数仓已按初诊来源规则算好(L2),一经判定不改。
 *   数据来源:fact_client_out.primary_category(一级)/ sub_category(二级)→ 摄入 PAC 副表
 *     patient_profiles.acquisition_channel(PAC 立柱标准枚举)/ acquisition_sub(host 原值)。
 *   本特征只把副表立柱值投影成标签(host→PAC 归一已在 assembler enum_mapping 完成)。
 *   无渠道值(老数据/未摄入)→ 不打标签(返回 null)。
 */
@Injectable()
export class AcquisitionChannelFeatureExtractor implements FeatureExtractor {
  readonly key = PersonaFeatureKey.ACQUISITION_CHANNEL;

  extract(ctx: FeatureExtractorContext): PersonaFeatureDraft | null {
    const channel = ctx.profile?.acquisitionChannel ?? null;
    if (!channel) return null;
    const sub = ctx.profile?.acquisitionSub ?? null;
    const label = acquisitionChannelLabelZh(channel);
    return {
      key: this.key,
      description: sub ? `${label} · ${sub}` : label,
      score: null,
      data: { channel, label, sub },
      // 来自副表立柱(源自 fact_client_out,非 fact 证据链)
      evidence: { factIds: [] },
    };
  }
}
