import { TransferOrder, TransferOrderData } from '../../../domain/aggregates/transfer-order.aggregate';

/**
 * TransferOrder Domain <-> Persistence 映射器
 */
export class TransferOrderMapper {
  /**
   * Prisma 模型 → Domain 聚合根
   */
  static toDomain(prismaOrder: any): TransferOrder {
    const data: TransferOrderData = {
      id: prismaOrder.id,
      transferOrderNo: prismaOrder.transferOrderNo,
      sellerUserId: prismaOrder.sellerUserId,
      sellerAccountSequence: prismaOrder.sellerAccountSequence,
      buyerUserId: prismaOrder.buyerUserId,
      buyerAccountSequence: prismaOrder.buyerAccountSequence,
      sourceOrderNo: prismaOrder.sourceOrderNo,
      sourceAdoptionId: prismaOrder.sourceAdoptionId,
      treeCount: prismaOrder.treeCount,
      contributionPerTree: prismaOrder.contributionPerTree.toString(),
      originalAdoptionDate: prismaOrder.originalAdoptionDate,
      originalExpireDate: prismaOrder.originalExpireDate,
      selectedProvince: prismaOrder.selectedProvince,
      selectedCity: prismaOrder.selectedCity,
      transferPrice: prismaOrder.transferPrice.toString(),
      platformFeeRate: prismaOrder.platformFeeRate.toString(),
      platformFeeAmount: prismaOrder.platformFeeAmount.toString(),
      sellerReceiveAmount: prismaOrder.sellerReceiveAmount.toString(),
      status: prismaOrder.status,
      sagaStep: prismaOrder.sagaStep,
      failReason: prismaOrder.failReason,
      retryCount: prismaOrder.retryCount,
      sellerConfirmedAt: prismaOrder.sellerConfirmedAt,
      paymentFrozenAt: prismaOrder.paymentFrozenAt,
      treesLockedAt: prismaOrder.treesLockedAt,
      ownershipTransferredAt: prismaOrder.ownershipTransferredAt,
      contributionAdjustedAt: prismaOrder.contributionAdjustedAt,
      statsUpdatedAt: prismaOrder.statsUpdatedAt,
      paymentSettledAt: prismaOrder.paymentSettledAt,
      completedAt: prismaOrder.completedAt,
      cancelledAt: prismaOrder.cancelledAt,
      rolledBackAt: prismaOrder.rolledBackAt,
      createdAt: prismaOrder.createdAt,
      updatedAt: prismaOrder.updatedAt,
    };

    return TransferOrder.reconstitute(data);
  }

  /**
   * Domain 聚合根 → Prisma 数据
   */
  static toPersistence(order: TransferOrder) {
    return {
      transferOrderNo: order.transferOrderNo,
      sellerUserId: order.sellerUserId,
      sellerAccountSequence: order.sellerAccountSequence,
      buyerUserId: order.buyerUserId,
      buyerAccountSequence: order.buyerAccountSequence,
      sourceOrderNo: order.sourceOrderNo,
      sourceAdoptionId: order.sourceAdoptionId,
      treeCount: order.treeCount,
      contributionPerTree: order.contributionPerTree,
      originalAdoptionDate: order.originalAdoptionDate,
      originalExpireDate: order.originalExpireDate,
      selectedProvince: order.selectedProvince,
      selectedCity: order.selectedCity,
      transferPrice: order.transferPrice,
      platformFeeRate: order.platformFeeRate,
      platformFeeAmount: order.platformFeeAmount,
      sellerReceiveAmount: order.sellerReceiveAmount,
      status: order.status,
      sagaStep: order.sagaStep,
      failReason: order.failReason,
      retryCount: order.retryCount,
      sellerConfirmedAt: order.sellerConfirmedAt,
      paymentFrozenAt: order.paymentFrozenAt,
      treesLockedAt: order.treesLockedAt,
      ownershipTransferredAt: order.ownershipTransferredAt,
      contributionAdjustedAt: order.contributionAdjustedAt,
      statsUpdatedAt: order.statsUpdatedAt,
      paymentSettledAt: order.paymentSettledAt,
      completedAt: order.completedAt,
      cancelledAt: order.cancelledAt,
      rolledBackAt: order.rolledBackAt,
    };
  }
}
