import nodemailer from 'nodemailer';
import dotenv from 'dotenv';

dotenv.config();

console.log('🔍 Testing Contact Email Configuration...\n');

// Check environment variables
console.log('Environment Variables:');
console.log('- GOOGLE_APP_EMAIL:', process.env.GOOGLE_APP_EMAIL);
console.log('- GOOGLE_APP_PASSCODE:', process.env.GOOGLE_APP_PASSCODE ? '✓ Set' : '✗ Missing');
console.log('- Recipient: cssawwwards@gmail.com\n');

const createTransporter = () => {
  return nodemailer.createTransport({
    host: "mail.cssawwwards.com",
    port: 465,
    secure: true,
    auth: {
      user: process.env.CPANEL_EMAIL || "contact@cssawwwards.com",
      pass: process.env.CPANEL_EMAIL_PASSWORD,
    },
    tls: {
      rejectUnauthorized: false // Bypass SSL certificate validation if needed
    },
    debug: true, // Enable debugging
    logger: true // Log output
  });
};

async function testContactEmail() {
  try {
    console.log('📧 Creating transporter...');
    const transporter = createTransporter();

    // Test connection
    console.log('🔌 Testing SMTP connection...');
    await transporter.verify();
    console.log('✅ SMTP connection verified!\n');

    // Send test email
    console.log('📤 Sending test contact email...');
    const mailOptions = {
      from: `"CSSAWWWARDS Contact Form Test" <${process.env.GOOGLE_APP_EMAIL}>`,
      to: 'cssawwwards@gmail.com',
      replyTo: 'test@example.com',
      subject: '[TEST] Contact Form Test - John Doe',
      html: `
        <div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto; padding: 20px;">
          <div style="background-color: #000; color: #fff; padding: 20px; text-align: center;">
            <h1>Test Contact Form Submission</h1>
          </div>
          <div style="background-color: #f9f9f9; padding: 30px; border: 1px solid #ddd;">
            <div style="margin-bottom: 20px;">
              <div style="font-weight: bold; color: #555; margin-bottom: 5px;">Subject:</div>
              <div style="background-color: #fff; padding: 10px; border-left: 3px solid #000;">Technical Problem</div>
            </div>
            <div style="margin-bottom: 20px;">
              <div style="font-weight: bold; color: #555; margin-bottom: 5px;">Name:</div>
              <div style="background-color: #fff; padding: 10px; border-left: 3px solid #000;">John Doe</div>
            </div>
            <div style="margin-bottom: 20px;">
              <div style="font-weight: bold; color: #555; margin-bottom: 5px;">Email:</div>
              <div style="background-color: #fff; padding: 10px; border-left: 3px solid #000;">test@example.com</div>
            </div>
            <div style="margin-bottom: 20px;">
              <div style="font-weight: bold; color: #555; margin-bottom: 5px;">Message:</div>
              <div style="background-color: #fff; padding: 10px; border-left: 3px solid #000;">This is a test message to verify the contact form email functionality is working correctly.</div>
            </div>
          </div>
          <div style="text-align: center; padding: 20px; color: #777; font-size: 12px;">
            <p>This is a test email from CSSAWWWARDS Contact Form</p>
          </div>
        </div>
      `,
      text: 'Subject: Technical Problem\nName: John Doe\nEmail: test@example.com\n\nMessage:\nThis is a test message to verify the contact form email functionality is working correctly.',
    };

    const info = await transporter.sendMail(mailOptions);
    console.log('✅ Test email sent successfully!');
    console.log('📧 Message ID:', info.messageId);
    console.log('📬 Check cssawwwards@gmail.com inbox\n');

  } catch (error) {
    console.error('❌ Test failed:', error.message);
    console.error('Full error:', error);
  }
}

testContactEmail();
