const { v4: uuidv4 } = require('uuid');

class ChainTransaction {
  constructor({ sourceService, actionType, actorId, targetId, amount, metadata } = {}) {
    this.id = uuidv4();
    this.sourceService = sourceService || 'UNKNOWN';
    this.actionType = actionType || 'GENERIC_ACTION';
    this.actorId = actorId || null;
    this.targetId = targetId || null;
    this.amount = Number(amount || 0);
    this.metadata = metadata || {};
    this.status = 'PENDING_MOCK';
    this.txHash = null;
    this.networkId = null;
    this.blockHeight = null;
    this.createdAt = new Date().toISOString();
    this.confirmedAt = null;
  }

  toChainPayload() {
    return {
      id: this.id,
      sourceService: this.sourceService,
      actionType: this.actionType,
      actorId: this.actorId,
      targetId: this.targetId,
      amount: this.amount,
      metadata: this.metadata,
      createdAt: this.createdAt
    };
  }

  markConfirmed(chainResult) {
    this.status = chainResult.status;
    this.txHash = chainResult.txHash;
    this.networkId = chainResult.networkId;
    this.blockHeight = chainResult.blockHeight;
    this.confirmedAt = chainResult.confirmedAt;
  }
}

module.exports = ChainTransaction;
