/**
 * Test email with IP address directly (bypass DNS)
 */

const nodemailer = require('nodemailer');

const config = {
  host: '3.209.246.195', // IP directe de smtp.mailtrap.io
  port: 2525,
  secure: false,
  auth: {
    user: '2597bd31d265eb',
    pass: 'cd126234193c89',
  },
  connectionTimeout: 10000,
  greetingTimeout: 10000,
  socketTimeout: 30000,
  tls: {
    rejectUnauthorized: false,
    servername: 'smtp.mailtrap.io', // Important pour TLS
  },
};

console.log('🧪 Testing SMTP with IP address directly...');
console.log('Config:', {
  ...config,
  auth: { user: config.auth.user, pass: '***' },
});

const transporter = nodemailer.createTransport(config);

console.log('\n1️⃣ Verifying SMTP connection...');

transporter
  .verify()
  .then(() => {
    console.log('✅ SMTP connection verified!');
    console.log('\n2️⃣ Sending test email...');

    return transporter.sendMail({
      from: 'noreply@xpeditis.com',
      to: 'test@example.com',
      subject: 'Test Xpeditis - Envoi Direct IP',
      html: "<h1>✅ Email envoyé avec succès!</h1><p>Ce test utilise l'IP directe pour contourner le DNS.</p>",
    });
  })
  .then(info => {
    console.log('✅ Email sent successfully!');
    console.log('📧 Message ID:', info.messageId);
    console.log('📬 Response:', info.response);
    console.log('\n🎉 SUCCESS! Email sending works with IP directly.');
    process.exit(0);
  })
  .catch(error => {
    console.error('\n❌ ERROR:', error.message);
    console.error('Code:', error.code);
    console.error('Command:', error.command);

    if (error.code === 'EAUTH') {
      console.error('\n⚠️  Authentication failed - credentials may be invalid');
    } else if (error.code === 'ETIMEDOUT' || error.code === 'ECONNREFUSED') {
      console.error('\n⚠️  Connection failed - firewall or network issue');
    }

    process.exit(1);
  });
