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

/**
 * gender 性别(A.1.2)— 规则层,snapshot
 *
 * 口径(图):client_gender 直接映射 "M"/"男"→男性;"F"/"女"→女性;其他→未知。
 *   数据来源:现 PAC patient.gender 自映射;宿主 CDP 给 client_gender 后切宿主值。
 *   标签释义:客户生理性别,影响沟通话术与项目推荐。
 *   三值标签(含未知)→ 始终打标签,不返回 null。
 */
@Injectable()
export class GenderFeatureExtractor implements FeatureExtractor {
  readonly key = PersonaFeatureKey.GENDER;

  private static map(raw: string | null): { code: string; zh: string } {
    const s = String(raw ?? '').trim().toUpperCase();
    if (s === 'M' || s === '男' || s === 'MALE') return { code: 'male', zh: '男性' };
    if (s === 'F' || s === '女' || s === 'FEMALE') return { code: 'female', zh: '女性' };
    return { code: 'unknown', zh: '未知' };
  }

  extract(ctx: FeatureExtractorContext): PersonaFeatureDraft {
    const g = GenderFeatureExtractor.map(ctx.patient.gender);
    return {
      key: this.key,
      description: g.zh,
      score: null,
      data: { gender: g.code, label: g.zh },
      // 来自 patient 主档 gender(非 fact)
      evidence: { factIds: [] }, // 证据=patient.gender(主档,非 fact)
    };
  }
}
