/**
 * Live smoke test for EMAIL_REDIRECT_TO.
 *
 * Actually calls `sendEmail()` (the centralized helper used by every
 * outbound email path in the app) with a fake real-client address. The
 * SMTP transporter is monkey-patched to capture the message instead of
 * actually delivering it, so this is safe to run anywhere.
 *
 * Prints the captured `to` + `subject` so the operator can see with their
 * own eyes that the redirect happened. Exits non-zero if the redirect
 * failed for any reason.
 *
 * Usage:
 *   pnpm tsx scripts/smoke-test-redirect.ts
 */
import 'dotenv/config';

async function main() {
  const expectedRedirect = process.env.EMAIL_REDIRECT_TO;
  if (!expectedRedirect) {
    console.error('FAIL: EMAIL_REDIRECT_TO is not set in env. Set it before running this test.');
    process.exit(1);
  }

  console.log(`[smoke] EMAIL_REDIRECT_TO = ${expectedRedirect}`);
  console.log('');

  // Monkey-patch nodemailer's createTransport so we capture the call
  // without actually delivering. This is the same pattern the unit
  // tests use, but at the live import-time level so we're testing the
  // exact code path that runs in production.
  const nodemailer = await import('nodemailer');
  const captured: Array<{ to: unknown; subject: unknown; from: unknown }> = [];
  const originalCreateTransport = nodemailer.default.createTransport;
  nodemailer.default.createTransport = (() => ({
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    sendMail: async (msg: any) => {
      captured.push({ to: msg.to, subject: msg.subject, from: msg.from });
      return { messageId: '<smoke@test>', accepted: [msg.to], rejected: [] };
    },
  })) as unknown as typeof nodemailer.default.createTransport;

  // Now import sendEmail (gets the patched transporter).
  const { sendEmail } = await import('@/lib/email');

  const realClientEmail = 'real-client-DO-NOT-EMAIL@example.test';
  const realSubject = 'Important: Your contract is ready';

  console.log('[smoke] calling sendEmail(...) with:');
  console.log(`         to:      ${realClientEmail}`);
  console.log(`         subject: "${realSubject}"`);
  console.log('');

  await sendEmail(realClientEmail, realSubject, '<p>Body unused for this smoke.</p>');

  // Restore the original transport (be a good citizen).
  nodemailer.default.createTransport = originalCreateTransport;

  console.log('[smoke] captured outbound message:');
  console.log(`         to:      ${captured[0]?.to}`);
  console.log(`         subject: "${captured[0]?.subject}"`);
  console.log(`         from:    ${captured[0]?.from}`);
  console.log('');

  // Assertions
  let pass = true;

  if (captured.length !== 1) {
    console.error(`FAIL: expected exactly 1 sendMail call, got ${captured.length}`);
    pass = false;
  }

  if (captured[0]?.to !== expectedRedirect) {
    console.error(
      `FAIL: outbound "to" was "${captured[0]?.to}", expected the redirect address "${expectedRedirect}"`,
    );
    pass = false;
  }

  if (
    typeof captured[0]?.subject !== 'string' ||
    !captured[0].subject.startsWith(`[redirected from ${realClientEmail}]`)
  ) {
    console.error(
      `FAIL: subject did not get the [redirected from <orig>] prefix. Got: "${captured[0]?.subject}"`,
    );
    pass = false;
  }

  if (pass) {
    console.log('PASS: EMAIL_REDIRECT_TO is intercepting outbound email correctly.');
    console.log(
      '      The "to" header matches the redirect, and the original recipient is preserved in the subject.',
    );
    process.exit(0);
  } else {
    console.error('');
    console.error('Smoke test FAILED. Do not import production data until this is fixed.');
    process.exit(1);
  }
}

main().catch((err) => {
  console.error('FATAL:', err);
  process.exit(1);
});
