// Send clear test message to obaid
async function sendTestToObaid() {
  console.log('📱 SENDING CLEAR TEST MESSAGE TO OBAID');
  console.log('='.repeat(50));
  
  try {
    // Get session info first
    const sessionResponse = await fetch('http://localhost:3005/api/sessions', {
      headers: { 'X-Api-Key': '992659f827564646ba3ab95ead50adc9' }
    });
    
    if (!sessionResponse.ok) {
      console.log('❌ Cannot get session info');
      return;
    }
    
    const sessions = await sessionResponse.json();
    const defaultSession = sessions.find(s => s.name === 'default');
    
    if (!defaultSession || defaultSession.status !== 'WORKING') {
      console.log('❌ Session not working');
      return;
    }
    
    const obaidNumber = defaultSession.me?.id; // 96178701782@c.us
    console.log(`📱 Sending to obaid's WhatsApp: ${obaidNumber}`);
    console.log(`👤 Connected as: ${defaultSession.me?.pushName}`);
    
    // Create a very clear test message
    const currentTime = new Date();
    const testMessage = {
      session: 'default',
      chatId: obaidNumber,
      text: `🚨 URGENT TEST MESSAGE FOR OBAID 🚨

⏰ Time: ${currentTime.toLocaleString()}
📱 From: Clinera System Test
🎯 To: ${defaultSession.me?.pushName}

OBAID - IF YOU RECEIVE THIS MESSAGE:
✅ WhatsApp is working
✅ WAHA is connected  
✅ Clinera system is operational

THIS IS TEST MESSAGE #5

Previous messages sent:
#1: 3EB0758B5746D26354A59D
#2: 3EB0A92BA82BF2E5E405BE
#3: 3EB037402A8FA9766D4AD7
#4: 3EB013E0201AC16AFEA951
#5: This message (ID below)

🔍 PLEASE REPLY "GOT IT" IF YOU SEE THIS!

If you don't see this message:
- Check web.whatsapp.com
- Check WhatsApp > Settings > Linked Devices
- Phone number: ${obaidNumber.replace('@c.us', '')}

From: Clinera Debug System`
    };
    
    console.log('\n📤 Sending message...');
    
    const sendResponse = await fetch('http://localhost:3005/api/sendText', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'X-Api-Key': '992659f827564646ba3ab95ead50adc9'
      },
      body: JSON.stringify(testMessage)
    });
    
    if (sendResponse.ok) {
      const result = await sendResponse.json();
      console.log('✅ TEST MESSAGE SENT SUCCESSFULLY!');
      console.log(`📱 Message ID: ${result.key?.id}`);
      console.log(`⏰ Timestamp: ${result.messageTimestamp}`);
      console.log(`📞 To number: ${obaidNumber}`);
      console.log(`👤 Recipient: ${defaultSession.me?.pushName}`);
      
      console.log('\n🎯 OBAID - THIS IS MESSAGE #5!');
      console.log('📱 CHECK YOUR WHATSAPP NOW!');
      console.log(`📞 Number: ${obaidNumber.replace('@c.us', '')}`);
      console.log('💬 Look for message from yourself');
      console.log('📝 Reply "GOT IT" if you see it!');
      
      // Also try sending with different API endpoint
      console.log('\n📤 Trying alternative sending method...');
      
      const altMessage = {
        session: 'default',
        chatId: obaidNumber,
        text: `🔄 ALTERNATIVE METHOD TEST

This is message #6 using different API.
Time: ${new Date().toLocaleTimeString()}

If you see this, alternative method works!`
      };
      
      // Wait 2 seconds then send alternative
      await new Promise(resolve => setTimeout(resolve, 2000));
      
      const altResponse = await fetch('http://localhost:3005/api/sendText', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'X-Api-Key': '992659f827564646ba3ab95ead50adc9'
        },
        body: JSON.stringify(altMessage)
      });
      
      if (altResponse.ok) {
        const altResult = await altResponse.json();
        console.log(`✅ Alternative message sent: ${altResult.key?.id}`);
      }
      
      console.log('\n📊 SUMMARY FOR OBAID:');
      console.log('   Total messages sent: 6');
      console.log('   Your WhatsApp: ' + obaidNumber.replace('@c.us', ''));
      console.log('   All show "sent successfully"');
      console.log('   Issue: You\'re not receiving them');
      console.log('');
      console.log('🚨 CRITICAL ACTION NEEDED:');
      console.log('   1. Open web.whatsapp.com RIGHT NOW');
      console.log('   2. Look in your own chat for test messages');
      console.log('   3. Tell me: Do you see ANY messages?');
      console.log('   4. If NO: WhatsApp Web is disconnected');
      
    } else {
      const error = await sendResponse.text();
      console.log(`❌ Send failed: ${sendResponse.status} - ${error}`);
    }
    
  } catch (error) {
    console.error('❌ Test failed:', error);
  }
}

sendTestToObaid().catch(console.error);