/**
 * 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」→ 宿主导出时已从结算头反填真 patient_id(inline);PAC 直用
const patient_settlement_spec = [
  { id: 'sp1', is_refund: '1', settlement_id: 's3', patient_id: '666716' }, // 退费明细(宿主已反填真 patient_id)
  { id: 'sp2', is_refund: '0', settlement_id: 's1', patient_id: '666716' }, // 非退费
];
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_rows', where: { is_refund: { equals: '1' } } },
  { 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 退费明细患者 = 宿主反填的真 patient_id(spec.patient_id 直用,不再跨表 lookup)', () => {
    const rows = (out['refund_item_rows'] ?? []) as Array<Record<string, unknown>>;
    expect(rows.map((r) => r['patient_id'])).toEqual(['666716']);
  });

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