/**
 * FRIDAY 结算/病历 WHERE 迁移到 PAC transforms —— 验证"导原表 → PAC 侧 filter 切分"正确。
 *
 * 背景:业务 WHERE 从 export.sh 全部搬进 manifest transforms(单一真理源)。
 * 本测用合成的**原表**数据(全 status + 正负金额)喂 TransformEngine,断言四处切分:
 *   ① 消费  status∈{1,3} 且 receivable≥0(负额被 gte 挡)
 *   ② 退费① status=4 ∪ (status=3 且 receivable<0)  ← filter×2 + union
 *   ③ 退费明细 is_refund=1
 *   ④ 病历 status∈{3,4} 排草稿
 *
 * 跑:pnpm --filter @pac/service test -- friday-settlement-where
 */
import { TransformEngine } from '../src/modules/sync/transforms/transform-engine';
import type { TransformsConfig } from '../src/modules/sync/transforms/transforms.schema';

const engine = new TransformEngine();

// 合成原表(CSV 源天然全字符串;med_emr_info 是 JSON,status 用数字测 in 的 String 归一)
const patient_settlement = [
  { uuid: 's1', status: '1', receivable_this: '100', patient_id: '666716' }, // 消费
  { uuid: 's3', status: '3', receivable_this: '50', patient_id: '666716' }, // 消费(status3 正额)
  { uuid: 's1neg', status: '1', receivable_this: '-5', patient_id: '666716' }, // 消费但金额<0 → 被 gte 挡
  { uuid: 's4', status: '4', receivable_this: '-30', patient_id: '666716' }, // 退费(status4 整单反向)
  { uuid: 's3neg', status: '3', receivable_this: '-20', patient_id: '666716' }, // 退费(status3 负额表达)
  { uuid: 's2', status: '2', receivable_this: '10', patient_id: '666716' }, // 草稿/其他 → 都不收
];
// spec.patient_id 是「诊所本地垃圾 id」(元和王永品牌实证)→ 退费明细身份须从父结算头 lookup 纠正
const patient_settlement_spec = [
  { id: 'sp1', is_refund: '1', settlement_id: 's3', patient_id: '2' }, // 退费明细(本地 id 2,应被纠成 666716)
  { id: 'sp2', is_refund: '0', settlement_id: 's1', patient_id: '2' }, // 非退费
];
const med_emr_info = [
  { emr_sub_id: 'e3', status: 3 }, // 正式
  { emr_sub_id: 'e4', status: 4 }, // 正式
  { emr_sub_id: 'e2', status: 2 }, // 草稿 → 排除
];

const transforms = [
  { kind: 'filter', input: 'patient_settlement', output: '_pay_hdr', where: { status: { in: ['1', '3'] }, receivable_this: { gte: 0 } } },
  { kind: 'filter', input: 'patient_settlement', output: '_refund_status4', where: { status: { equals: '4' } } },
  { kind: 'filter', input: 'patient_settlement', output: '_refund_neg3', where: { status: { equals: '3' }, receivable_this: { lt: 0 } } },
  { kind: 'union', inputs: ['_refund_status4', '_refund_neg3'], output: 'refund_full_rows' },
  { kind: 'filter', input: 'patient_settlement_spec', output: '_refund_item_raw', where: { is_refund: { equals: '1' } } },
  { kind: 'lookup', input: '_refund_item_raw', output: 'refund_item_rows', from: 'patient_settlement', left_key: 'settlement_id', right_key: 'uuid', select: { owner_patient_id: 'patient_id' } },
  { kind: 'filter', input: 'med_emr_info', output: '_emr_official', where: { status: { in: ['3', '4'] } } },
] as unknown as TransformsConfig;

describe('FRIDAY 结算/病历 WHERE 迁移(导原表 + PAC 切分)', () => {
  const out = engine.run({
    tables: { patient_settlement, patient_settlement_spec, med_emr_info },
    transforms,
  });
  const pick = (rows: unknown[], key: string) =>
    (rows as Array<Record<string, unknown>>).map((r) => r[key]).sort();

  it('① 消费:status∈{1,3} 且 receivable≥0(负额 s1neg 被挡,s2/s4 不在)', () => {
    expect(pick(out['_pay_hdr'] ?? [], 'uuid')).toEqual(['s1', 's3']);
  });

  it('② 退费①:status=4 ∪ (status=3 且金额<0)', () => {
    expect(pick(out['refund_full_rows'] ?? [], 'uuid')).toEqual(['s3neg', 's4']);
  });

  it('③ 退费明细:is_refund=1', () => {
    expect(pick(out['refund_item_rows'] ?? [], 'id')).toEqual(['sp1']);
  });

  it('③.1 退费明细身份纠正:spec 本地 id 2 → 从父结算头继承真 patient_id 666716', () => {
    const rows = (out['refund_item_rows'] ?? []) as Array<Record<string, unknown>>;
    expect(rows.map((r) => r['owner_patient_id'])).toEqual(['666716']);
  });

  it('④ 病历:status∈{3,4} 排草稿 e2', () => {
    expect(pick(out['_emr_official'] ?? [], 'emr_sub_id')).toEqual(['e3', 'e4']);
  });
});
