import nodemailer from 'nodemailer';

const transporter = nodemailer.createTransport({
  host: process.env.SMTP_HOST || 'localhost',
  port: parseInt(process.env.SMTP_PORT || '587'),
  secure: process.env.SMTP_SECURE === 'true',
  auth: process.env.SMTP_USER ? {
    user: process.env.SMTP_USER,
    pass: process.env.SMTP_PASS
  } : undefined
});

interface EmailOptions {
  to: string;
  subject: string;
  template: string;
  data?: Record<string, any>;
}

const templates: Record<string, (data: any) => string> = {
  welcome: (data) => `
    <div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
      <div style="background: #2D6A4F; padding: 30px; text-align: center;">
        <h1 style="color: white; margin: 0;">🥑 Welcome to TheKetoBay!</h1>
      </div>
      <div style="padding: 30px; background: #f9f9f9;">
        <h2>Hey ${data.name}! 👋</h2>
        <p>Welcome to the #1 keto platform. We're excited to help you on your keto journey!</p>
        <p>Here's what you can do right now:</p>
        <ul>
          <li>🍽️ Browse our <a href="${process.env.FRONTEND_URL}/plans">keto meal plans</a></li>
          <li>🤖 Try our <a href="${process.env.FRONTEND_URL}/ai-planner">AI Keto Planner</a> (free!)</li>
          <li>📊 Start tracking your <a href="${process.env.FRONTEND_URL}/tracker">macros & progress</a></li>
        </ul>
        <div style="background: #D8F3DC; border-radius: 8px; padding: 20px; margin-top: 20px;">
          <p style="margin: 0;"><strong>🎁 Your free 3-day starter plan is waiting!</strong></p>
          <a href="${process.env.FRONTEND_URL}/plans/3-day-keto-starter" style="display: inline-block; margin-top: 10px; background: #2D6A4F; color: white; padding: 10px 20px; border-radius: 5px; text-decoration: none;">Download Free Plan</a>
        </div>
      </div>
      <div style="padding: 20px; text-align: center; color: #999; font-size: 12px;">
        <p>TheKetoBay · <a href="${process.env.FRONTEND_URL}/unsubscribe">Unsubscribe</a></p>
      </div>
    </div>
  `,

  'password-reset': (data) => `
    <div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
      <div style="background: #2D6A4F; padding: 30px; text-align: center;">
        <h1 style="color: white; margin: 0;">🔐 Password Reset</h1>
      </div>
      <div style="padding: 30px;">
        <p>You requested a password reset for your TheKetoBay account.</p>
        <p>Click the button below to reset your password. This link expires in 1 hour.</p>
        <a href="${data.resetUrl}" style="display: inline-block; margin: 20px 0; background: #2D6A4F; color: white; padding: 12px 24px; border-radius: 5px; text-decoration: none; font-size: 16px;">Reset Password</a>
        <p style="color: #999; font-size: 12px;">If you didn't request this, ignore this email.</p>
      </div>
    </div>
  `,

  'purchase-confirmation': (data) => `
    <div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
      <div style="background: #2D6A4F; padding: 30px; text-align: center;">
        <h1 style="color: white; margin: 0;">✅ Purchase Confirmed!</h1>
      </div>
      <div style="padding: 30px;">
        <p>Thanks for purchasing <strong>${data.planTitle}</strong>!</p>
        <p>Your plan is now available in your dashboard.</p>
        <a href="${process.env.FRONTEND_URL}/dashboard" style="display: inline-block; margin: 20px 0; background: #2D6A4F; color: white; padding: 12px 24px; border-radius: 5px; text-decoration: none;">Go to Dashboard</a>
      </div>
    </div>
  `,

  'free-plan': (data) => `
    <div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
      <div style="background: #2D6A4F; padding: 30px; text-align: center;">
        <h1 style="color: white; margin: 0;">🥑 Your Free 3-Day Keto Plan</h1>
      </div>
      <div style="padding: 30px;">
        <p>Here's your free keto starter plan as promised!</p>
        <a href="${data.downloadUrl}" style="display: inline-block; margin: 20px 0; background: #52B788; color: white; padding: 12px 24px; border-radius: 5px; text-decoration: none;">📥 Download Your Plan</a>
        <p>Want more? Check out our full keto plan collection:</p>
        <a href="${process.env.FRONTEND_URL}/plans" style="color: #2D6A4F;">Browse All Plans →</a>
      </div>
    </div>
  `
};

export async function sendEmail({ to, subject, template, data = {} }: EmailOptions) {
  const html = templates[template]?.(data) || `<p>${JSON.stringify(data)}</p>`;

  await transporter.sendMail({
    from: `"TheKetoBay" <${process.env.SMTP_FROM || 'noreply@theketobay.com'}>`,
    to,
    subject,
    html
  });
}
