// Test the enhanced trade processing logic
const dummyTrade = {
  id: 'test',
  side: 'BUY',
  amount: 2.04,
  price: 100.0522427503856,
  entryPrice: null,
  exitPrice: null,
  profit: null,
  status: 'COMPLETED',
  createdAt: new Date('2025-07-21T08:17:07Z'),
  closedAt: null
};

console.log('Original trade data:');
console.log(JSON.stringify(dummyTrade, null, 2));

// Apply the enhanced logic
const currentPrice = 175.82;
const entryPrice = dummyTrade.entryPrice || dummyTrade.price;
let exitPrice = dummyTrade.exitPrice;
let calculatedProfit = dummyTrade.profit;

// If exit price is null but trade is completed, try to calculate from profit
if (dummyTrade.status === 'COMPLETED' && !exitPrice && calculatedProfit !== null && calculatedProfit !== undefined) {
  if (dummyTrade.side === 'BUY') {
    exitPrice = entryPrice + (calculatedProfit / dummyTrade.amount);
  } else {
    exitPrice = entryPrice - (calculatedProfit / dummyTrade.amount);
  }
}

// If profit is null but we have both prices, calculate profit
if (dummyTrade.status === 'COMPLETED' && (calculatedProfit === null || calculatedProfit === undefined) && exitPrice && entryPrice) {
  if (dummyTrade.side === 'BUY') {
    calculatedProfit = (exitPrice - entryPrice) * dummyTrade.amount;
  } else {
    calculatedProfit = (entryPrice - exitPrice) * dummyTrade.amount;
  }
}

// Determine result based on actual profit
let result = 'ACTIVE';
if (dummyTrade.status === 'COMPLETED') {
  if (calculatedProfit === null || calculatedProfit === undefined) {
    result = 'UNKNOWN'; // When we truly don't have enough data
  } else if (Math.abs(calculatedProfit) < 0.01) { // Within 1 cent
    result = 'BREAKEVEN';
  } else if (calculatedProfit > 0) {
    result = 'WIN';
  } else {
    result = 'LOSS';
  }
}

console.log('\nProcessed trade data:');
console.log('Entry Price:', entryPrice);
console.log('Exit Price:', exitPrice);
console.log('Calculated Profit:', calculatedProfit);
console.log('Result:', result);

console.log('\nThis trade would be marked as UNKNOWN because it has no exit price or profit data.');
