/**
 * Synor Price Feed Simulator
 * Simulates realistic price movements for testnet
 *
 * Features:
 * - Realistic volatility patterns
 * - Correlated price movements (BTC drives alts)
 * - Random whale trades / spikes
 */

const REDIS_URL = process.env.REDIS_URL || 'redis://localhost:6379';
const UPDATE_INTERVAL = parseInt(process.env.UPDATE_INTERVAL_MS) || 500;
const VOLATILITY = parseFloat(process.env.VOLATILITY) || 0.001;

// Initial prices
const prices = {
  'BTC/USD': parseFloat(process.env.INITIAL_BTC_PRICE) || 45000,
  'ETH/USD': parseFloat(process.env.INITIAL_ETH_PRICE) || 2500,
  'SYNOR/USD': parseFloat(process.env.INITIAL_SYNOR_PRICE) || 1.50,
  'SOL/USD': 95,
  'ATOM/USD': 8.50,
  'OSMO/USD': 0.75,
  'AVAX/USD': 35,
  'DOT/USD': 6.50,
};

// Correlation to BTC (1.0 = perfect correlation)
const correlations = {
  'BTC/USD': 1.0,
  'ETH/USD': 0.85,
  'SYNOR/USD': 0.4,
  'SOL/USD': 0.75,
  'ATOM/USD': 0.6,
  'OSMO/USD': 0.5,
  'AVAX/USD': 0.7,
  'DOT/USD': 0.65,
};

// Generate correlated random walk
function generatePriceMove(symbol, btcMove) {
  const correlation = correlations[symbol] || 0.5;
  const independentMove = (Math.random() - 0.5) * 2;
  const correlatedMove = btcMove * correlation + independentMove * (1 - correlation);

  // Occasionally add larger moves (whale activity)
  const whaleChance = Math.random();
  if (whaleChance > 0.995) {
    return correlatedMove * 10; // 10x normal move
  } else if (whaleChance > 0.98) {
    return correlatedMove * 3; // 3x normal move
  }

  return correlatedMove;
}

// Update all prices
function updatePrices() {
  // BTC moves first
  const btcMove = (Math.random() - 0.5) * 2;
  const baseVolatility = VOLATILITY;

  Object.keys(prices).forEach(symbol => {
    const move = generatePriceMove(symbol, btcMove);
    const volatility = baseVolatility * (symbol === 'BTC/USD' ? 1 : 1.5); // Alts more volatile
    const priceChange = move * volatility * prices[symbol];
    prices[symbol] = Math.max(prices[symbol] + priceChange, 0.0001);
  });

  // Log price updates
  const timestamp = new Date().toISOString();
  console.log(`[${timestamp}] Price Update:`);
  console.log(`  BTC: $${prices['BTC/USD'].toFixed(2)}`);
  console.log(`  ETH: $${prices['ETH/USD'].toFixed(2)}`);
  console.log(`  SYNOR: $${prices['SYNOR/USD'].toFixed(4)}`);
}

// Start simulation
console.log('Synor Price Feed Simulator');
console.log('========================');
console.log(`Update Interval: ${UPDATE_INTERVAL}ms`);
console.log(`Base Volatility: ${VOLATILITY * 100}%`);
console.log('');
console.log('Initial Prices:');
Object.entries(prices).forEach(([symbol, price]) => {
  console.log(`  ${symbol}: $${price.toFixed(4)}`);
});
console.log('');
console.log('Starting price simulation...');

setInterval(updatePrices, UPDATE_INTERVAL);
