import { emailService } from '../src/services/email/email.service.js';

const recipients = ['ivan@horuxfin.com', 'carlos@horuxfin.com'];

async function sendAllSamples() {
  for (const to of recipients) {
    console.log(`\n=== Enviando a ${to} ===`);

    // 1. Welcome
    console.log('1/6 Bienvenida...');
    await emailService.sendWelcome(to, {
      nombre: 'Ivan Alcaraz',
      email: 'ivan@horuxfin.com',
      tempPassword: 'TempPass123!',
    });

    // 2. FIEL notification (goes to ADMIN_EMAIL, but we override for test)
    console.log('2/6 Notificación FIEL...');
    // Send directly since sendFielNotification goes to admin
    const { fielNotificationEmail } = await import('../src/services/email/templates/fiel-notification.js');
    const { createTransport } = await import('nodemailer');
    const { env } = await import('../src/config/env.js');
    const transport = createTransport({
      host: env.SMTP_HOST,
      port: parseInt(env.SMTP_PORT),
      secure: false,
      auth: { user: env.SMTP_USER, pass: env.SMTP_PASS },
    });
    const fielHtml = fielNotificationEmail({
      clienteNombre: 'Horux 360',
      clienteRfc: 'CAS200101XXX',
    });
    await transport.sendMail({
      from: env.SMTP_FROM,
      to,
      subject: '[Horux 360] subió su FIEL (MUESTRA)',
      html: fielHtml,
    });

    // 3. Payment confirmed
    console.log('3/6 Pago confirmado...');
    await emailService.sendPaymentConfirmed(to, {
      nombre: 'Ivan Alcaraz',
      amount: 1499,
      plan: 'Enterprise',
      date: '16 de marzo de 2026',
    });

    // 4. Payment failed
    console.log('4/6 Pago fallido...');
    const { paymentFailedEmail } = await import('../src/services/email/templates/payment-failed.js');
    const failedHtml = paymentFailedEmail({
      nombre: 'Ivan Alcaraz',
      amount: 1499,
      plan: 'Enterprise',
    });
    await transport.sendMail({
      from: env.SMTP_FROM,
      to,
      subject: 'Problema con tu pago - Horux360 (MUESTRA)',
      html: failedHtml,
    });

    // 5. Subscription expiring
    console.log('5/6 Suscripción por vencer...');
    await emailService.sendSubscriptionExpiring(to, {
      nombre: 'Ivan Alcaraz',
      plan: 'Enterprise',
      expiresAt: '21 de marzo de 2026',
    });

    // 6. Subscription cancelled
    console.log('6/6 Suscripción cancelada...');
    const { subscriptionCancelledEmail } = await import('../src/services/email/templates/subscription-cancelled.js');
    const cancelledHtml = subscriptionCancelledEmail({
      nombre: 'Ivan Alcaraz',
      plan: 'Enterprise',
    });
    await transport.sendMail({
      from: env.SMTP_FROM,
      to,
      subject: 'Suscripción cancelada - Horux360 (MUESTRA)',
      html: cancelledHtml,
    });

    console.log(`Listo: 6 correos enviados a ${to}`);
  }

  console.log('\n=== Todos los correos enviados ===');
  process.exit(0);
}

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