import { RewardLedgerEntry as PrismaRewardLedgerEntry, Prisma } from '@prisma/client';
import { RewardLedgerEntry } from '../../../domain/aggregates/reward-ledger-entry/reward-ledger-entry.aggregate';
import { RewardSource } from '../../../domain/value-objects/reward-source.vo';
import { RewardStatus } from '../../../domain/value-objects/reward-status.enum';
import { RightType } from '../../../domain/value-objects/right-type.enum';

export class RewardLedgerEntryMapper {
  static toDomain(raw: PrismaRewardLedgerEntry): RewardLedgerEntry {
    return RewardLedgerEntry.reconstitute({
      id: raw.id,
      userId: raw.userId,
      accountSequence: raw.accountSequence,
      rewardSource: RewardSource.create(
        raw.rightType as RightType,
        raw.sourceOrderNo,
        raw.sourceUserId,
        raw.sourceAccountSequence,
      ),
      usdtAmount: Number(raw.usdtAmount),
      hashpowerAmount: Number(raw.hashpowerAmount),
      rewardStatus: raw.rewardStatus as RewardStatus,
      createdAt: raw.createdAt,
      expireAt: raw.expireAt,
      claimedAt: raw.claimedAt,
      settledAt: raw.settledAt,
      expiredAt: raw.expiredAt,
      memo: raw.memo || '',
    });
  }

  static toPersistence(entry: RewardLedgerEntry) {
    return {
      id: entry.id || undefined,
      userId: entry.userId,
      accountSequence: entry.accountSequence,
      sourceOrderNo: entry.rewardSource.sourceOrderNo,
      sourceUserId: entry.rewardSource.sourceUserId,
      sourceAccountSequence: entry.rewardSource.sourceAccountSequence,
      rightType: entry.rewardSource.rightType,
      usdtAmount: new Prisma.Decimal(entry.usdtAmount.amount),
      hashpowerAmount: new Prisma.Decimal(entry.hashpowerAmount.value),
      rewardStatus: entry.rewardStatus,
      createdAt: entry.createdAt,
      expireAt: entry.expireAt,
      claimedAt: entry.claimedAt,
      settledAt: entry.settledAt,
      expiredAt: entry.expiredAt,
      memo: entry.memo,
    };
  }
}
