import { config } from '../src/config/index.js';
import { RiskEngine } from '../src/services/riskEngine.js';
import { SignalDirection, MarketContext, Candle } from '../src/strategies/rules/types.js';
import logger from '../src/utils/logger.js';

// Helper to generate candles with specific volatility
function generateCandles(basePrice: number, volatility: number): Candle[] {
    const candles: Candle[] = [];
    for (let i = 0; i < 20; i++) {
        candles.push({
            timestamp: Date.now() - (i * 3600000),
            open: basePrice,
            high: basePrice + volatility,
            low: basePrice - volatility,
            close: basePrice,
            volume: 1000
        });
    }
    return candles;
}

async function testRiskScenarios() {
    const riskEngine = new RiskEngine();
    const basePrice = 90000; // BTC Example

    console.log(`--- RISK ENGINE SCENARIO TEST ---`);
    console.log(`Config Balance: $${config.TOTAL_CAPITAL}`);
    console.log(`Risk Per Trade: ${config.PRO_STRATEGY.PARAMETERS.RISK_PER_TRADE * 100}%`);
    console.log(`Target Risk Amount: $${config.TOTAL_CAPITAL * config.PRO_STRATEGY.PARAMETERS.RISK_PER_TRADE}\n`);

    // --- Scenario 1: Calm Market (Low Volatility) ---
    const calmContext: MarketContext = {
        currentPrice: basePrice,
        candles4h: [],
        candles1h: generateCandles(basePrice, 450), // ATR will be around 900 (High-Low)
        candles15m: [],
        change24h: 0,
        changeToday: 0,
        session: 'NY',
        isMajorSession: true,
        volatility: 'Low',
        latestSignal: SignalDirection.NONE
    };

    const calmProfile = await riskEngine.calculateRiskProfile('BTC/USDT', SignalDirection.BUY, calmContext);

    console.log(`[SCENARIO A: CALM]`);
    if (calmProfile) {
        console.log(`ATR-based Stop Distance: $${(basePrice - calmProfile.stopLoss).toFixed(2)}`);
        console.log(`Position Size: ${calmProfile.positionSize.toFixed(4)} BTC`);
        console.log(`Stop Loss Price: $${calmProfile.stopLoss.toFixed(2)}`);
        console.log(`Total $ Risked: $${calmProfile.riskAmount.toFixed(2)}`);
    }

    console.log('\n---------------------------\n');

    // --- Scenario 2: Volatile Market (High Volatility) ---
    const volatileContext: MarketContext = {
        currentPrice: basePrice,
        candles4h: [],
        candles1h: generateCandles(basePrice, 900), // ATR will be around 1800
        candles15m: [],
        change24h: 0,
        changeToday: 0,
        session: 'NY',
        isMajorSession: true,
        volatility: 'High',
        latestSignal: SignalDirection.NONE
    };

    const volatileProfile = await riskEngine.calculateRiskProfile('BTC/USDT', SignalDirection.BUY, volatileContext);

    console.log(`[SCENARIO B: VOLATILE]`);
    if (volatileProfile) {
        console.log(`ATR-based Stop Distance: $${(basePrice - volatileProfile.stopLoss).toFixed(2)}`);
        console.log(`Position Size: ${volatileProfile.positionSize.toFixed(4)} BTC (Bigger Stop = Smaller Size)`);
        console.log(`Stop Loss Price: $${volatileProfile.stopLoss.toFixed(2)}`);
        console.log(`Total $ Risked: $${volatileProfile.riskAmount.toFixed(2)}`);
    }

    console.log(`\n--- TEST COMPLETE ---`);
}

testRiskScenarios().catch(console.error);
