/**
 * Email Template Testing Script
 * 
 * Test email templates with nodemailer test account
 * Preview URL will be shown in console
 */

import nodemailer from 'nodemailer';
import {
  getRequestCreatedEmail,
  getApprovalRequestEmail,
  getMultiApproverRequestEmail,
  RequestCreatedData,
  ApprovalRequestData,
  MultiApproverRequestData
} from './index';

/**
 * Send test email and get preview URL
 */
async function sendTestEmail() {
  try {
    console.log('🚀 Creating nodemailer test account...');

    // Create a test account automatically (free, no signup needed)
    const testAccount = await nodemailer.createTestAccount();

    console.log('✅ Test account created:', testAccount.user);

    // Create transporter with test SMTP details
    const transporter = nodemailer.createTransport({
      host: testAccount.smtp.host,
      port: testAccount.smtp.port,
      secure: testAccount.smtp.secure,
      auth: {
        user: testAccount.user,
        pass: testAccount.pass
      }
    });

    // Sample data for Request Created email
    const requestCreatedData: RequestCreatedData = {
      recipientName: 'John Doe',
      requestId: 'REQ-2025-12-0013',
      requestTitle: 'New Equipment Purchase Request',
      initiatorName: 'John Doe',
      firstApproverName: 'Jane Smith',
      requestType: 'Purchase',
      priority: 'HIGH',
      requestDate: 'Dec 04, 2025',
      requestTime: '02:30 PM',
      totalApprovers: 3,
      expectedTAT: 48,
      viewDetailsLink: 'http://localhost:3000/request/REQ-2025-12-0013',
      companyName: 'Royal Enfield'
    };

    // Sample data for Approval Request email
    const approvalRequestData: ApprovalRequestData = {
      recipientName: 'Jane Smith',
      requestId: 'REQ-2025-12-0013',
      approverName: 'Jane Smith',
      initiatorName: 'John Doe',
      requestType: 'Purchase',
      requestDescription: 'Requesting approval for new equipment purchase worth $5,000. This includes 2 new motorcycles for the testing department.',
      priority: 'HIGH',
      requestDate: 'Dec 04, 2025',
      requestTime: '02:30 PM',
      viewDetailsLink: 'http://localhost:3000/request/REQ-2025-12-0013',
      companyName: 'Royal Enfield'
    };

    // Sample data for Multi-Approver Request email
    const multiApproverData: MultiApproverRequestData = {
      ...approvalRequestData,
      approverLevel: 2,
      totalApprovers: 3,
      approversList: [
        {
          name: 'Sarah Johnson',
          status: 'approved',
          date: 'Dec 03, 2025',
          levelNumber: 1
        },
        {
          name: 'Jane Smith',
          status: 'current',
          levelNumber: 2
        },
        {
          name: 'Michael Brown',
          status: 'awaiting',
          levelNumber: 3
        }
      ]
    };

    console.log('\n📧 Sending test emails...\n');

    // Test 1: Request Created Email
    const html1 = getRequestCreatedEmail(requestCreatedData);
    const info1 = await transporter.sendMail({
      from: '"Royal Enfield Workflow" <noreply@royalenfield.com>',
      to: 'initiator@example.com',
      subject: '[REQ-2025-12-0013] Request Created Successfully',
      html: html1
    });

    console.log('✅ Test 1: Request Created Email');
    console.log('   Preview URL:', nodemailer.getTestMessageUrl(info1));
    console.log('');

    // Test 2: Approval Request Email (Single)
    const html2 = getApprovalRequestEmail(approvalRequestData);
    const info2 = await transporter.sendMail({
      from: '"Royal Enfield Workflow" <noreply@royalenfield.com>',
      to: 'approver@example.com',
      subject: '[REQ-2025-12-0013] Approval Request - Action Required',
      html: html2
    });

    console.log('✅ Test 2: Approval Request Email');
    console.log('   Preview URL:', nodemailer.getTestMessageUrl(info2));
    console.log('');

    // Test 3: Multi-Approver Request Email
    const html3 = getMultiApproverRequestEmail(multiApproverData);
    const info3 = await transporter.sendMail({
      from: '"Royal Enfield Workflow" <noreply@royalenfield.com>',
      to: 'approver-level2@example.com',
      subject: '[REQ-2025-12-0013] Multi-Level Approval Request - Your Turn',
      html: html3
    });

    console.log('✅ Test 3: Multi-Approver Request Email');
    console.log('   Preview URL:', nodemailer.getTestMessageUrl(info3));
    console.log('');

    console.log('🎉 All test emails sent successfully!');
    console.log('\n💡 Click the Preview URLs above to view the emails in your browser');
    console.log('📌 These are real previews hosted by Ethereal Email (nodemailer test service)');

  } catch (error) {
    console.error('❌ Error sending test email:', error);
    throw error;
  }
}

// Run the test
sendTestEmail()
  .then(() => {
    console.log('\n✅ Test completed successfully');
    process.exit(0);
  })
  .catch((error) => {
    console.error('\n❌ Test failed:', error);
    process.exit(1);
  });

