// Mock realistic data for the portfolio

const initialAssets = [
  { id: 'btc',  name: 'Bitcoin',        ticker: 'BTC',  type: 'cripto',  qty: 0.00412, buyPrice: 58200, currentPrice: 67140, change24: 2.14 },
  { id: 'baba', name: 'Alibaba Group',  ticker: 'BABA', type: 'accion',  qty: 1.6,     buyPrice: 78.30, currentPrice: 82.55, change24: -0.42 },
  { id: 'bnb',  name: 'BNB',            ticker: 'BNB',  type: 'cripto',  qty: 0.214,   buyPrice: 560.10, currentPrice: 592.80, change24: 1.02 },
  { id: 'qqq',  name: 'Invesco QQQ',    ticker: 'QQQ',  type: 'etf',     qty: 0.21,    buyPrice: 455.20, currentPrice: 471.60, change24: 0.58 },
  { id: 'spy',  name: 'S&P 500 ETF',    ticker: 'SPY',  type: 'etf',     qty: 0.16,    buyPrice: 514.80, currentPrice: 538.40, change24: 0.31 },
  { id: 'nvda', name: 'NVIDIA',         ticker: 'NVDA', type: 'accion',  qty: 0.42,    buyPrice: 118.40, currentPrice: 134.90, change24: 3.12 },
  { id: 'eth',  name: 'Ethereum',       ticker: 'ETH',  type: 'cripto',  qty: 0.021,   buyPrice: 2420.00, currentPrice: 2615.50, change24: -1.18 },
];

// History: 30 days of values (seeded-ish pseudo-random growth from ~$760 to $956)
function buildHistory(days) {
  const out = [];
  const start = 760;
  const end = 956.08;
  // Shape: steep rise first 10 days, plateau, small dip and recover
  for (let i = 0; i < days; i++) {
    const t = i / (days - 1);
    // logistic-ish ramp + small noise
    const base = start + (end - start) * (1 - Math.exp(-3.2 * t));
    const noise = Math.sin(i * 0.9) * 4 + Math.cos(i * 1.7) * 2.5;
    const dip = i > days * 0.7 && i < days * 0.82 ? -8 : 0;
    const d = new Date();
    d.setDate(d.getDate() - (days - 1 - i));
    out.push({ date: d, value: +(base + noise + dip).toFixed(2) });
  }
  return out;
}

const history30 = buildHistory(30);
const history7  = buildHistory(7).map((p, i, arr) => ({ ...p, value: 940 + Math.sin(i * 1.1) * 8 + i * 1.1 }));
const history24 = Array.from({ length: 24 }, (_, i) => {
  const d = new Date();
  d.setHours(d.getHours() - (23 - i));
  return { date: d, value: +(950 + Math.sin(i * 0.5) * 3.5 + i * 0.22).toFixed(2) };
});

window.initialAssets = initialAssets;
window.historyData = { '24H': history24, '7D': history7, '30D': history30 };

window.formatUSD = (v, digits = 2) => {
  return '$' + Number(v).toLocaleString('en-US', { minimumFractionDigits: digits, maximumFractionDigits: digits });
};
// formateo de precio unitario con decimales auto-escalados según la magnitud,
// para que monedas tipo PEPE (~$0.0000039) no se muestren como $0.0000.
window.formatPrice = (v) => {
  const n = Number(v) || 0;
  const abs = Math.abs(n);
  let digits;
  if (abs >= 1) digits = 2;
  else if (abs >= 0.01) digits = 4;
  else if (abs >= 0.0001) digits = 6;
  else digits = 8;
  return '$' + n.toLocaleString('en-US', { minimumFractionDigits: digits, maximumFractionDigits: digits });
};
window.formatPct = (v, digits = 2) => {
  const sign = v > 0 ? '+' : '';
  return `${sign}${Number(v).toFixed(digits)}%`;
};
window.typeLabel = (t) => ({ cripto: 'Cripto', accion: 'Acción', etf: 'ETF' }[t] || t);
