import { Injectable, Logger } from '@nestjs/common';
import { Action, FactKind, FactStatus, FactType } from '@pac/types';
import type { FactDraft, Parser, ParserContext } from './parser.interface';

/**
 * PaymentParser — `payment_received` 解析器
 *
 * 一条 payment transaction 衍生:
 *   - 1 个 `payment_record`(actual)— 收款事实
 *     subject_id = `payment_record:{externalId}`
 *     content.amount = cents int(FieldMapper.normalize 已转换)
 *     content.order_external_id 关联医嘱(可选)
 *
 * 副作用(应用层处理,不在 parser 做):
 *   - 关联的 charge_order_record / payment_plan 状态机:
 *     比对 sum(payments.amount) vs charge_order.total_amount:
 *       全付 → charge_order_record status=fulfilled,payment_plan supersede 走人
 *       部分 → charge_order_record status=active(host 侧通常会用 partial_paid 推一条 update)
 *     这层依赖跨 fact 的聚合,由 W3 PersonaService / PlanService 在 read path 算,
 *     不在 parser 写入侧做(避免 fact 之间紧耦合)。
 *
 * Persona `value` 累计消费用 payment_record.amount 求和(扣 refund_record)。
 */
@Injectable()
export class PaymentParser implements Parser {
  readonly name = Action.PAYMENT_RECEIVED;
  private readonly logger = new Logger(PaymentParser.name);

  parse(ctx: ParserContext): FactDraft[] {
    const c = ctx.canonicalRow;
    const externalId = String(c.externalId ?? ctx.transaction.subjectId).trim();
    if (!externalId) {
      this.logger.warn(
        `payment parser: missing externalId for transaction=${ctx.transaction.id};skip`,
      );
      return [];
    }

    const amount = Number(c.amount ?? 0); // cents
    const paidAt = c.paidAt ? new Date(c.paidAt as string) : null;
    const encounterExternalId =
      (c.encounterExternalId as string | undefined) ?? null;
    const orderExternalId = (c.orderExternalId as string | undefined) ?? null;
    const method = (c.method as string | undefined) ?? null;
    const doctorId = c.doctorId ? String(c.doctorId) : null; // host Int64,0/null → null
    const currency = (c.currency as string | undefined) ?? 'CNY';

    return [
      {
        subjectId: `${FactType.PAYMENT_RECORD}:${externalId}`,
        kind: FactKind.ACTUAL,
        type: FactType.PAYMENT_RECORD,
        status: FactStatus.ACTIVE,
        clinicId: ctx.transaction.clinicId,
        occurredAt: paidAt,
        title: `收款 ¥${(amount / 100).toFixed(2)}${method ? '(' + method + ')' : ''}`,
        summary: orderExternalId ? `关联医嘱:${orderExternalId}` : null,
        content: {
          payment_external_id: externalId,
          amount_cents: amount,
          channel: method,
          doctor_id: doctorId,
          encounter_external_id: encounterExternalId,
          related_order_external_id: orderExternalId,
        },
      },
    ];
  }
}
