#!/usr/bin/env node
/**
 * Test Interview Invitation Flow
 *
 * This script tests the complete flow:
 * 1. Staff sends interview invitation
 * 2. Email is sent to applicant with booking link
 * 3. Token is saved to database
 *
 * Usage: node scripts/test-interview-invite-flow.mjs
 */

import { sendInterviewInvitationEmail } from '../lib/email.ts';

console.log('🧪 Testing Interview Invitation Email Flow\n');

// Test data
const testData = {
  applicantEmail: 'test-applicant@mailinator.com',
  applicantName: 'Test Applicant',
  jobTitle: 'Senior Full-Stack Developer',
  jobTitleAr: 'مطور ويب متقدم',
  bookingToken: 'test_token_abc123xyz789',
  expiryDate: new Date(Date.now() + 72 * 60 * 60 * 1000).toISOString(), // 3 days from now
  language: 'en',
};

console.log('📧 Sending test interview invitation email...');
console.log('To:', testData.applicantEmail);
console.log('Applicant:', testData.applicantName);
console.log('Job:', testData.jobTitle);
console.log('Token:', testData.bookingToken);
console.log('Expires:', new Date(testData.expiryDate).toLocaleString());
console.log('');

try {
  const result = await sendInterviewInvitationEmail(
    testData.applicantEmail,
    testData.applicantName,
    testData.jobTitle,
    testData.jobTitleAr,
    testData.bookingToken,
    testData.expiryDate,
    testData.language
  );

  if (result) {
    console.log('✅ SUCCESS! Email sent successfully');
    console.log('');
    console.log('📬 Check MailHog to see the email:');
    console.log('   http://localhost:8025');
    console.log('');
    console.log('🔗 The applicant would receive this booking link:');
    console.log(`   http://localhost:9000/en/careers/interview/schedule?token=${testData.bookingToken}`);
    console.log('');
    console.log('Next steps:');
    console.log('1. Open MailHog at http://localhost:8025');
    console.log('2. Find the email to test-applicant@mailinator.com');
    console.log('3. Verify the email contains:');
    console.log('   - Job title: Senior Full-Stack Developer');
    console.log('   - Applicant name: Test Applicant');
    console.log('   - Booking link with token');
    console.log('   - 3-day expiry notice');
    console.log('   - "Schedule Your Interview" button');
  } else {
    console.log('❌ FAILED! Email was not sent');
    console.log('Check your email configuration in .env.local');
  }
} catch (error) {
  console.error('❌ ERROR!', error);
  console.log('');
  console.log('Troubleshooting:');
  console.log('1. Make sure MailHog is running (port 1025)');
  console.log('2. Check .env.local has correct SMTP settings');
  console.log('3. Verify lib/email.ts exports sendInterviewInvitationEmail');
}

console.log('');
console.log('✨ Test complete!');
