#!/usr/bin/env node

/**
 * Test script to verify timeframe mapping and interval selection fix
 */

async function testTimeframeMapping() {
  console.log('🧪 Testing timeframe mapping fix...\n')
  
  // Test the mapping logic from the fixed code
  const timeframeMap = {
    '1': ['1', '1m', '1min'],
    '1m': ['1', '1m', '1min'],
    '5': ['5', '5m', '5min'],
    '5m': ['5', '5m', '5min'],
    '15': ['15', '15m', '15min'],
    '15m': ['15', '15m', '15min'],
    '30': ['30', '30m', '30min'],
    '30m': ['30', '30m', '30min'],
    // For 1 hour - prioritize minute values first to avoid confusion
    '60': ['60', '60m', '1h', '1H'],
    '1h': ['60', '60m', '1h', '1H'], 
    '1H': ['60', '60m', '1h', '1H'],
    // For 4 hours - CRITICAL: prioritize 240 minutes to avoid "4min" confusion
    '240': ['240', '240m', '4h', '4H'],
    '4h': ['240', '240m', '4h', '4H'], // Always try 240 minutes FIRST
    '4H': ['240', '240m', '4h', '4H'],
    // Add other common hour timeframes
    '2h': ['120', '120m', '2h', '2H'],
    '2H': ['120', '120m', '2h', '2H'],
    '6h': ['360', '360m', '6h', '6H'],
    '6H': ['360', '360m', '6h', '6H'],
    '12h': ['720', '720m', '12h', '12H'],
    '12H': ['720', '720m', '12h', '12H'],
    // Daily and weekly
    '1D': ['1D', 'D', 'daily', '1d'],
    '1d': ['1D', 'D', 'daily', '1d'],
    '1W': ['1W', 'W', 'weekly', '1w'],
    '1w': ['1W', 'W', 'weekly', '1w']
  }
  
  const minutesMap = {
    '4h': '240',
    '4H': '240',
    '240': '240',
    '2h': '120',
    '2H': '120',
    '6h': '360',
    '6H': '360',
    '12h': '720',
    '12H': '720',
    '1h': '60',
    '1H': '60',
    '60': '60'
  }
  
  // Test cases that were problematic
  const testCases = ['4h', '4H', '1h', '1H', '2h', '6h', '12h']
  
  console.log('📊 Timeframe mapping test results:')
  console.log('=====================================')
  
  for (const timeframe of testCases) {
    const mappedValues = timeframeMap[timeframe] || [timeframe]
    const minutesValue = minutesMap[timeframe]
    
    console.log(`\n🔍 Input: "${timeframe}"`)
    console.log(`   🎯 Primary attempts: ${mappedValues.join(', ')}`)
    console.log(`   🔢 Fallback minutes: ${minutesValue || 'N/A'}`)
    console.log(`   ✅ First attempt: "${mappedValues[0]}" (should avoid confusion)`)
  }
  
  console.log('\n' + '='.repeat(50))
  console.log('🎯 KEY IMPROVEMENT: For "4h" input:')
  console.log('   ❌ Old: Would try "4h", "4H" first (interpreted as 4 minutes)')
  console.log('   ✅ New: Tries "240", "240m" first (240 minutes = 4 hours)')
  console.log('   🔄 Fallback: If all else fails, enters "240" in custom input')
  
  console.log('\n✨ This should fix the interval selection bug!')
}

// Run the test
testTimeframeMapping().catch(console.error)
