
import { ConnectorFactory } from '../src/connectors/factory.js';
import { config } from '../src/config/index.js';
import { Indicators } from '../src/utils/indicators.js';
import logger from '../src/utils/logger.js';

async function verifyBTC() {
    const exchange = ConnectorFactory.getCustomConnector('ccxt', '', ''); // Using CCXT (Binance/Kraken) for data
    const symbol = 'BTC/USDT';

    console.log(`\n--- 📊 BTC ANALYSIS VERIFICATION (Last 4 Hours) ---`);

    // 1. Fetch 4H Data for Trend Bias
    const candles4h = await exchange.fetchOHLCV(symbol, '4h', 100);
    const ema50_4h = Indicators.calculateEMA(candles4h.map(c => c.close), 50);
    const ema200_4h = Indicators.calculateEMA(candles4h.map(c => c.close), 200);
    const last4h = candles4h[candles4h.length - 1];

    console.log(`4H Context:`);
    console.log(`   Current Price: $${last4h.close}`);
    console.log(`   EMA50 (4H):    $${ema50_4h.toFixed(2)}`);
    console.log(`   EMA200 (4H):   $${ema200_4h.toFixed(2)}`);
    console.log(`   Trend Bias:    ${last4h.close > ema50_4h ? 'BULLISH' : 'BEARISH'} (Price vs EMA50)`);

    // 2. Fetch 1H Data for Momentum
    const candles1h = await exchange.fetchOHLCV(symbol, '1h', 50);
    const rsi1h = Indicators.calculateRSI(candles1h.map(c => c.close), 14);
    const ema20_1h = Indicators.calculateEMA(candles1h.map(c => c.close), 20);

    console.log(`\n1H Context:`);
    console.log(`   RSI (1H):      ${rsi1h.toFixed(2)}`);
    console.log(`   EMA20 (1H):    $${ema20_1h.toFixed(2)}`);
    console.log(`   Momentum:      ${rsi1h > 50 ? 'BULLISH' : 'BEARISH'}`);

    // 3. Performance Check (Last 2 Hours)
    const candles15m = await exchange.fetchOHLCV(symbol, '15m', 12);
    const startPrice = candles15m[0].open;
    const endPrice = candles15m[candles15m.length - 1].close;
    const change = ((endPrice - startPrice) / startPrice) * 100;

    console.log(`\nRecent Performance (Last 3 Hours - 15m candles):`);
    console.log(`   Opening: $${startPrice.toFixed(2)}`);
    console.log(`   Closing: $${endPrice.toFixed(2)}`);
    console.log(`   Change:  ${change.toFixed(2)}%`);

    console.log(`\n--- BOT LOGIC CHECK ---`);
    if (last4h.close < ema50_4h) {
        console.log(`⚠️ BOT IS BEARISH because Price ($${last4h.close}) is BELOW EMA50 ($${ema50_4h.toFixed(2)}) on 4H.`);
        console.log(`   Even if the last 2 hours were up, the "Higher Timeframe Trend" is still down.`);
    } else {
        console.log(`✅ BOT SHOULD BE BULLISH.`);
    }
}

verifyBTC();
