// Send another test message to obaid's correct number
async function sendAnotherTest() {
  console.log('📱 SENDING ANOTHER TEST MESSAGE TO OBAID');
  console.log('='.repeat(50));
  
  try {
    const currentTime = new Date();
    const testMessage = {
      session: 'default',
      chatId: '201066544750@c.us', // obaid's correct WhatsApp
      text: `🧪 ANOTHER TEST MESSAGE #2

Hi Obaid!

This is test message #2 to confirm the system is still working perfectly.

📱 Your correct WhatsApp: +201066544750
⏰ Time: ${currentTime.toLocaleString()}
🎯 Message purpose: System confirmation test

Previous messages sent to your number:
✅ Message 1: Wrong number explanation + booking confirmation
✅ Message 2: This message

If you receive this, the reminder system is 100% operational and ready for real appointments!

🚀 Status: READY FOR APPOINTMENT CREATION

Next step: Create appointment in Clinera UI and you'll get booking confirmation instantly!

From: Clinera Test System 📲`
    };
    
    console.log('📤 Sending test message...');
    console.log(`   To: +201066544750 (obaid's WhatsApp)`);
    console.log(`   Time: ${currentTime.toLocaleString()}`);
    
    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('✅ ANOTHER TEST MESSAGE SENT SUCCESSFULLY!');
      console.log(`📨 Message ID: ${result.key?.id}`);
      console.log(`📞 To: +201066544750`);
      console.log(`⏰ Timestamp: ${result.messageTimestamp}`);
      console.log(`🔄 Status: ${result.status}`);
      
      console.log('\n🎯 OBAID - CHECK YOUR WHATSAPP NOW!');
      console.log('   Phone: +201066544750');
      console.log('   Look for: "ANOTHER TEST MESSAGE #2"');
      console.log('   Content: System confirmation + status');
      console.log('');
      console.log('✅ If you see this message:');
      console.log('   - Reminder system is working');
      console.log('   - Correct phone number confirmed');
      console.log('   - Ready for real appointments');
      console.log('');
      console.log('📋 Next: Create appointment in UI and get instant booking confirmation!');
      
      // Also log this in database for tracking
      const { PrismaClient } = require('@prisma/client');
      const prisma = new PrismaClient();
      
      try {
        await prisma.messageLog.create({
          data: {
            clinicId: (await prisma.clinic.findFirst()).id,
            phone: '+201066544750',
            direction: 'outgoing',
            message: testMessage.text,
            status: 'sent',
            wahaMessageId: result.key?.id || null,
            sentAt: new Date(),
          },
        });
        console.log('✅ Message logged in database');
        await prisma.$disconnect();
      } catch (dbError) {
        console.log('⚠️ Database logging failed (message still sent)');
      }
      
    } else {
      const error = await sendResponse.text();
      console.log(`❌ Message send failed:`);
      console.log(`   Status: ${sendResponse.status}`);
      console.log(`   Error: ${error}`);
      
      console.log('\n🔧 If send failed:');
      console.log('   - WAHA might need restart: docker restart waha');
      console.log('   - WhatsApp Web might be disconnected');
      console.log('   - Check: http://localhost:3005/api/default/auth/qr');
    }
    
  } catch (error) {
    console.error('❌ Test failed:', error);
  }
}

sendAnotherTest().catch(console.error);