export function buildStatsWhere(period) {
  const now = new Date();
  let since = null;
  if (period === 'today') {
    since = new Date(now);
    since.setHours(0, 0, 0, 0);
  } else if (period === 'week') {
    since = new Date(now);
    since.setDate(since.getDate() - 7);
  } else if (period === 'month') {
    since = new Date(now);
    since.setMonth(since.getMonth() - 1);
  } else if (period === '30days') {
    since = new Date(now);
    since.setDate(since.getDate() - 30);
  } else if (period === '7days') {
    since = new Date(now);
    since.setDate(since.getDate() - 7);
  }
  return since;
}

export function computeSignalStats(rows) {
  const closed = rows.filter((r) => r.status === 'closed');
  const profit = closed.filter((r) => r.result === 'profit').length;
  const loss = closed.filter((r) => r.result === 'loss').length;
  const total = closed.length;
  const successRate = total > 0 ? Math.round((profit / total) * 10000) / 100 : 0;
  return { total, profit, loss, successRate, active: rows.filter((r) => r.status === 'active').length };
}
