import { DirectionTracker, SignalType } from '../src/strategies/directionTracker.js';
import { Notifier } from '../src/services/notifier.js';
import logger from '../src/utils/logger.js';

async function simulateSignalChange() {
    const tracker = new DirectionTracker();
    const notifier = new Notifier();

    logger.info('--- Starting Signal Change Simulation ---');

    // 1. Create Neutral/None state (Price = EMA, RSI = 50)
    // We need 20 candles
    const basePrice = 50000;
    const neutralCandles = Array.from({ length: 20 }, (_, i) => ({
        close: basePrice,
        timestamp: Date.now() - (20 - i) * 60000
    }));

    logger.info('Phase 1: Neutral state...');
    tracker.calculateDirection(neutralCandles);

    // 2. Force a BUY signal (Price > EMA, RSI < 70)
    // Price jumps up, but RSI stays moderate
    const buyCandles = [...neutralCandles, { close: 51000, timestamp: Date.now() }];
    logger.info('Phase 2: Forcing BUY signal...');
    const buyResult = tracker.calculateDirection(buyCandles);

    if (buyResult.changed && buyResult.signal === SignalType.BUY) {
        const message = `🚨 *SIMULATION: BUY SIGNAL* 🚨\nAsset: BTC/USD (MOCK)\nPrice: 51000\nEMA-20: ${buyResult.ema.toFixed(2)}\nRSI-14: ${buyResult.rsi.toFixed(2)}\nLogic: Price > EMA and RSI < 70`;
        await notifier.sendAlert(message);
    }

    // 3. Force a SELL signal (Price < EMA, RSI > 30)
    // Price drops sharply
    const sellCandles = [...buyCandles.slice(1), { close: 49000, timestamp: Date.now() + 60000 }];
    logger.info('Phase 3: Forcing SELL signal...');
    const sellResult = tracker.calculateDirection(sellCandles);

    if (sellResult.changed && sellResult.signal === SignalType.SELL) {
        const message = `🚨 *SIMULATION: SELL SIGNAL* 🚨\nAsset: BTC/USD (MOCK)\nPrice: 49000\nEMA-20: ${sellResult.ema.toFixed(2)}\nRSI-14: ${sellResult.rsi.toFixed(2)}\nLogic: Price < EMA and RSI > 30`;
        await notifier.sendAlert(message);
    }

    logger.info('--- Simulation Complete ---');
}

simulateSignalChange().catch(err => logger.error(err));
