const BREVO_API_URL = 'https://api.brevo.com/v3/smtp/email';

/**
 * Send email via Brevo API
 */
async function sendEmail({ to, subject, htmlContent, textContent }) {
  const apiKey = process.env.BREVO_API_KEY;

  if (!apiKey) {
    console.error('BREVO_API_KEY not configured');
    throw new Error('Email service not configured');
  }

  const payload = {
    sender: {
      name: process.env.BREVO_SENDER_NAME || 'WellNuo',
      email: process.env.BREVO_SENDER_EMAIL || 'noreply@wellnuo.com'
    },
    to: [{ email: to }],
    subject,
    htmlContent,
    textContent
  };

  const response = await fetch(BREVO_API_URL, {
    method: 'POST',
    headers: {
      'accept': 'application/json',
      'api-key': apiKey,
      'content-type': 'application/json'
    },
    body: JSON.stringify(payload)
  });

  if (!response.ok) {
    const error = await response.text();
    console.error('Brevo API error:', error);
    throw new Error('Failed to send email');
  }

  return await response.json();
}

/**
 * Send password reset email
 */
async function sendPasswordResetEmail(email, resetToken) {
  const frontendUrl = process.env.FRONTEND_URL || 'https://wellnuo.smartlaunchhub.com';
  const resetLink = `${frontendUrl}/reset-password?token=${resetToken}`;

  const htmlContent = `
    <!DOCTYPE html>
    <html>
    <head>
      <style>
        body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; }
        .container { max-width: 600px; margin: 0 auto; padding: 20px; }
        .header { background: #4A90D9; color: white; padding: 20px; text-align: center; }
        .content { padding: 30px; background: #f9f9f9; }
        .button { display: inline-block; padding: 12px 30px; background: #4A90D9; color: white; text-decoration: none; border-radius: 5px; margin: 20px 0; }
        .footer { padding: 20px; text-align: center; font-size: 12px; color: #666; }
      </style>
    </head>
    <body>
      <div class="container">
        <div class="header">
          <h1>WellNuo</h1>
        </div>
        <div class="content">
          <h2>Password Reset Request</h2>
          <p>We received a request to reset your password. Click the button below to create a new password:</p>
          <p style="text-align: center;">
            <a href="${resetLink}" class="button">Reset Password</a>
          </p>
          <p>Or copy and paste this link into your browser:</p>
          <p style="word-break: break-all; color: #4A90D9;">${resetLink}</p>
          <p><strong>This link will expire in 1 hour.</strong></p>
          <p>If you didn't request a password reset, you can safely ignore this email.</p>
        </div>
        <div class="footer">
          <p>&copy; 2025 WellNuo - Elderly Care Monitoring</p>
        </div>
      </div>
    </body>
    </html>
  `;

  const textContent = `
WellNuo - Password Reset

We received a request to reset your password.

Click this link to reset your password:
${resetLink}

This link will expire in 1 hour.

If you didn't request a password reset, you can safely ignore this email.

WellNuo - Elderly Care Monitoring
  `;

  return sendEmail({
    to: email,
    subject: 'WellNuo - Password Reset Request',
    htmlContent,
    textContent
  });
}

/**
 * Send OTP code for email-based login
 */
async function sendOTPEmail(email, code, userName) {
  const htmlContent = `
    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body style="margin: 0; padding: 0; background-color: #FFFFFF; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Arial, sans-serif;">
      <table width="100%" cellspacing="0" cellpadding="0" style="background-color: #FFFFFF;">
        <tr>
          <td align="center" style="padding: 48px 24px;">
            <table width="100%" cellspacing="0" cellpadding="0" style="max-width: 400px;">

              <!-- Logo -->
              <tr>
                <td align="center" style="padding-bottom: 32px;">
                  <span style="font-size: 24px; font-weight: 700; color: #4A90D9;">WellNuo</span>
                </td>
              </tr>

              <!-- Title -->
              <tr>
                <td align="center" style="padding-bottom: 8px;">
                  <h1 style="margin: 0; font-size: 20px; font-weight: 600; color: #333333;">Your verification code</h1>
                </td>
              </tr>

              <!-- Subtitle -->
              <tr>
                <td align="center" style="padding-bottom: 32px;">
                  <p style="margin: 0; font-size: 14px; color: #666666;">
                    ${userName ? `Hi ${userName}, use` : 'Use'} this code to sign in
                  </p>
                </td>
              </tr>

              <!-- Code -->
              <tr>
                <td align="center" style="padding-bottom: 32px;">
                  <div style="display: inline-block; padding: 16px 32px; background-color: #F5F7FA; border-radius: 8px;">
                    <span style="font-size: 32px; font-weight: 700; letter-spacing: 6px; color: #333333;">${code}</span>
                  </div>
                </td>
              </tr>

              <!-- Expires -->
              <tr>
                <td align="center" style="padding-bottom: 32px;">
                  <p style="margin: 0; font-size: 13px; color: #999999;">
                    Code expires in 10 minutes
                  </p>
                </td>
              </tr>

              <!-- Divider -->
              <tr>
                <td style="padding-bottom: 24px;">
                  <div style="height: 1px; background-color: #E5E7EB;"></div>
                </td>
              </tr>

              <!-- Security note -->
              <tr>
                <td align="center">
                  <p style="margin: 0; font-size: 12px; color: #999999; line-height: 1.5;">
                    If you didn't request this code, you can safely ignore this email.
                  </p>
                </td>
              </tr>

            </table>
          </td>
        </tr>
      </table>
    </body>
    </html>
  `;

  const textContent = `
WellNuo - Your verification code

${userName ? `Hi ${userName}, use` : 'Use'} this code to sign in:

${code}

Code expires in 10 minutes.

If you didn't request this code, you can safely ignore this email.
  `;

  try {
    await sendEmail({
      to: email,
      subject: `${code} - Your WellNuo Login Code`,
      htmlContent,
      textContent
    });
    return true;
  } catch (error) {
    console.error('Failed to send OTP email:', error);
    return false;
  }
}

module.exports = {
  sendEmail,
  sendPasswordResetEmail,
  sendOTPEmail
};
