const { handleTicketing } = require('./features/ticketing');
const { updateUserData, getUserData } = require('./utils/user');

// Mock WhatsApp socket
const mockSock = {
  sendMessage: async (phone, message) => {
    console.log(`📤 Sending to ${phone}:`, message.text || message.caption || 'Media message');
    return { key: { id: 'test-message-id' } };
  }
};

async function testBuyAction() {
  const testPhone = '265991234570@s.whatsapp.net';
  
  console.log('🧪 Starting BUY action test for product_id 51...\n');
  
  try {
    // Set up user data with an event that has multiple ticket types in EVENT_DETAILS state
    await updateUserData(testPhone, {
      state: 'EVENT_DETAILS',
      name: 'Test User',
      currentEvent: {
        product_id: '51',
        product_name: 'Zisomo Gospel Concert (Children Ticket)',
        product_price: '100'
      }
    });
    
    // Get updated user data
    const userData = await getUserData(testPhone);
    console.log('Initial user data:', JSON.stringify(userData, null, 2));
    
    // Simulate user sending BUY action
    console.log('\n📋 Simulating user sending BUY action');
    await handleTicketing(mockSock, testPhone, userData, 'BUY');
    
    // Check the updated state
    const updatedUserData = await getUserData(testPhone);
    console.log('\n📋 Updated user data:', JSON.stringify(updatedUserData, null, 2));
    console.log('\n📋 Current state:', updatedUserData.state);
    
    console.log('\n✅ Test completed!');
    
  } catch (error) {
    console.error('❌ Test failed:', error);
  }
}

testBuyAction();