import { resolveKinRelationship } from '@pac/types';

/**
 * 亲戚关系方向的年龄纠正。
 *
 * 由来(2026-07-30 测试服全量实证):
 *   jvs-dw 43,279 条亲戚边里与年龄矛盾 428 条(1.0%),方向基本可信;
 *   friday 只看父母/子女**对**:mother↔child 326 对里只有 8 对方向是对的(2.5%)、
 *   father↔child 231 对里只有 9 对(3.9%)—— **97% 反了**。
 *   根因在 data/friday/assemblers/patient_relation.yaml 多做了一次逆关系映射。
 *
 * 用例锁的是这条纪律:**年龄说了算,但只在能判的时候判**。
 * 说错亲属关系(把女儿说成妈妈)客服在通话中会当场出丑,比不显示更糟。
 */
describe('resolveKinRelationship — 年龄定方向', () => {
  test('⭐ 标成妈妈但对方更年轻 → 纠正成子女(实测样本:秦佳47 / "妈妈"韩秦瑜21)', () => {
    expect(resolveKinRelationship('mother', 47, 21, '女')).toEqual({
      relationship: 'child',
      ageCorrected: true,
    });
  });

  test('⭐ 标成子女但对方更年长 → 按对方性别纠成父/母(实测样本:韩秦瑜21 / "子女"秦佳47)', () => {
    expect(resolveKinRelationship('child', 21, 47, '女')).toEqual({
      relationship: 'mother',
      ageCorrected: true,
    });
    expect(resolveKinRelationship('child', 35, 62, '男')).toEqual({
      relationship: 'father',
      ageCorrected: true,
    });
  });

  test('⭐ 纠成长辈但对方性别缺失 → 中性「parent」,不硬猜父还是母', () => {
    expect(resolveKinRelationship('child', 20, 50, null)).toEqual({
      relationship: 'parent',
      ageCorrected: true,
    });
    expect(resolveKinRelationship('child', 20, 50, '  ')).toEqual({
      relationship: 'parent',
      ageCorrected: true,
    });
  });

  test('方向本来就对 → 原样返回,不标纠正', () => {
    expect(resolveKinRelationship('mother', 34, 58, '女')).toEqual({
      relationship: 'mother',
      ageCorrected: false,
    });
    expect(resolveKinRelationship('child', 56, 7, '男')).toEqual({
      relationship: 'child',
      ageCorrected: false,
    });
  });

  test('祖辈 / 孙辈 同样纠(阈值只看谁更年长,不额外要求 30 岁差)', () => {
    expect(resolveKinRelationship('grandparent', 60, 20).relationship).toBe('grandchild');
    expect(resolveKinRelationship('grandchild', 20, 60).relationship).toBe('grandparent');
  });

  test('⭐ 同龄也算矛盾 —— 父母子女不可能同岁', () => {
    expect(resolveKinRelationship('mother', 40, 40, '女').ageCorrected).toBe(true);
    expect(resolveKinRelationship('child', 40, 40, '女').ageCorrected).toBe(true);
  });

  test('⭐ 配偶 / 兄弟姐妹是对称关系,没有方向可纠 —— 同岁配偶很正常,不许被误标', () => {
    expect(resolveKinRelationship('spouse', 64, 64, '男')).toEqual({
      relationship: 'spouse',
      ageCorrected: false,
    });
    expect(resolveKinRelationship('sibling', 30, 45, '女')).toEqual({
      relationship: 'sibling',
      ageCorrected: false,
    });
  });

  test('⭐ 任一方缺生日 → 原样返回,不猜(会显示的边有 98.8% 两边都有生日,剩下的宁可不判)', () => {
    expect(resolveKinRelationship('mother', null, 21, '女')).toEqual({
      relationship: 'mother',
      ageCorrected: false,
    });
    expect(resolveKinRelationship('mother', 47, null, '女')).toEqual({
      relationship: 'mother',
      ageCorrected: false,
    });
  });

  test('非亲戚码(friend/other)不参与纠正', () => {
    expect(resolveKinRelationship('friend', 20, 60)).toEqual({
      relationship: 'friend',
      ageCorrected: false,
    });
    expect(resolveKinRelationship('other', 60, 20)).toEqual({
      relationship: 'other',
      ageCorrected: false,
    });
  });
});
