/**
 * Test script for Phase 1 API endpoint
 * Run with: node test_phase1_api.js
 */

const PYTHON_SERVICE_URL = process.env.PYTHON_SERVICE_URL || 'http://localhost:8010';

async function testPhase1API() {
  console.log('🧪 Testing Phase 1 API Endpoint...\n');
  
  try {
    // Test 1: Health check
    console.log('1️⃣ Testing health endpoint...');
    const healthResponse = await fetch(`${PYTHON_SERVICE_URL}/health`);
    const healthData = await healthResponse.json();
    console.log('✅ Health:', healthData);
    console.log('');
    
    // Test 2: Options flow with Phase 1 fields
    console.log('2️⃣ Testing options flow endpoint...');
    const today = new Date().toISOString().split('T')[0];
    const yesterday = new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString().split('T')[0];
    
    const flowUrl = `${PYTHON_SERVICE_URL}/api/options-flow?start_date=${yesterday}&end_date=${today}&min_premium=80000`;
    console.log(`   URL: ${flowUrl}`);
    
    const flowResponse = await fetch(flowUrl);
    const flowData = await flowResponse.json();
    
    if (!flowData.success) {
      console.error('❌ API returned error:', flowData);
      return;
    }
    
    console.log(`✅ Response: ${flowData.count} records`);
    console.log('');
    
    // Test 3: Check for Phase 1 fields
    console.log('3️⃣ Checking for Phase 1 fields...');
    
    if (flowData.data && flowData.data.length > 0) {
      const firstRow = flowData.data[0];
      
      const phase1Fields = {
        'signal_tier': firstRow.signal_tier,
        'is_tradeable': firstRow.is_tradeable,
        'checklist_score': firstRow.checklist_score,
        'checklist_passed': firstRow.checklist_passed,
        'checklist_details': firstRow.checklist_details,
        'price_reaction_5m_pct': firstRow.price_reaction_5m_pct,
        'flow_led_to_move': firstRow.flow_led_to_move,
        'vwap_at_signal': firstRow.vwap_at_signal,
        'price_vs_vwap_pct': firstRow.price_vs_vwap_pct,
      };
      
      console.log('Phase 1 Fields in first row:');
      Object.entries(phase1Fields).forEach(([key, value]) => {
        const status = value !== undefined && value !== null ? '✅' : '❌';
        console.log(`   ${status} ${key}: ${JSON.stringify(value)}`);
      });
      
      // Count Tier-1 signals
      const tier1Count = flowData.data.filter(r => r.signal_tier === 'TIER_1').length;
      const checklistPassedCount = flowData.data.filter(r => r.checklist_passed === true).length;
      const flowLedToMoveCount = flowData.data.filter(r => r.flow_led_to_move === true).length;
      
      console.log('');
      console.log('📊 Phase 1 Statistics:');
      console.log(`   Tier-1 signals: ${tier1Count}/${flowData.count}`);
      console.log(`   Checklist passed: ${checklistPassedCount}/${flowData.count}`);
      console.log(`   Flow led to move: ${flowLedToMoveCount}/${flowData.count}`);
      
    } else {
      console.log('⚠️  No data returned - cannot verify Phase 1 fields');
    }
    
    console.log('');
    console.log('✅ Phase 1 API test complete!');
    
  } catch (error) {
    console.error('❌ Error testing API:', error.message);
    console.error('   Make sure the Python service is running on', PYTHON_SERVICE_URL);
  }
}

// Run the test
testPhase1API();

