All files / src/lib/integrations email.ts

97.5% Statements 899/922
93.61% Branches 44/47
100% Functions 15/15
97.5% Lines 899/922

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 9231x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 51x 51x 51x 51x 51x 51x                                               51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 1x 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 5x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 8x 8x 4x 8x 2x 2x 2x 8x 2x 8x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x  
import { logger } from '@/lib/logging';
 
/**
 * Email service configuration
 * In production, this would use Resend, SendGrid, or similar service
 * Currently uses a mock implementation for development
 */
 
interface EmailOptions {
  to: string;
  subject: string;
  html: string;
  text?: string;
}
 
/**
 * Send an email
 * In production, integrate with Resend, SendGrid, or your email provider
 */
export async function sendEmail(options: EmailOptions): Promise<boolean> {
  const { to, subject, html, text } = options;
 
  // Check if Resend API key is configured
  const resendApiKey = process.env.RESEND_API_KEY;
 
  if (resendApiKey && resendApiKey !== "mock") {
    try {
      // Dynamic import to avoid issues when Resend isn't installed
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      const resendModule = await import("resend" as any).catch(() => null);
      if (resendModule) {
        const resend = new resendModule.Resend(resendApiKey);
        const fromEmail = process.env.EMAIL_FROM || "noreply@eliteevents.com";

        await resend.emails.send({
          from: `Elite Events <${fromEmail}>`,
          to,
          subject,
          html,
          text: text || html.replace(/<[^>]*>/g, "")});

        logger.info(`Email sent successfully to ${to}`, { category: "EMAIL", subject });
        return true;
      }
    } catch (error) {
      logger.error(`Failed to send email to ${to}`, error instanceof Error ? error : new Error(String(error)), { category: "EMAIL" });
      return false;
    }
  }
 
  // Development mode: log email to console
  logger.info(`[DEV MODE] Email would be sent to ${to}`, {
    category: "EMAIL",
    subject,
    preview: html.substring(0, 200)});
 
  // In development, simulate successful send
  return true;
}
 
/**
 * Send password reset email
 */
export async function sendPasswordResetEmail(
  email: string,
  token: string
): Promise<boolean> {
  const baseUrl = process.env.NEXTAUTH_URL || "http://localhost:3000";
  const resetUrl = `${baseUrl}/reset-password?token=${token}`;
 
  const html = `
    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Reset Your Password</title>
    </head>
    <body style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; line-height: 1.6; padding: 20px; max-width: 600px; margin: 0 auto;">
      <div style="background-color: #f8f9fa; padding: 20px; border-radius: 8px;">
        <h1 style="color: #333; margin-bottom: 20px;">Password Reset Request</h1>
 
        <p style="color: #555; margin-bottom: 20px;">
          We received a request to reset your password for your Elite Events account.
          Click the button below to create a new password:
        </p>
 
        <a href="${resetUrl}"
           style="display: inline-block; background-color: #3b82f6; color: white; padding: 12px 24px; text-decoration: none; border-radius: 6px; font-weight: bold; margin-bottom: 20px;">
          Reset Password
        </a>
 
        <p style="color: #666; font-size: 14px; margin-top: 20px;">
          This link will expire in 1 hour for security reasons.
        </p>
 
        <p style="color: #666; font-size: 14px;">
          If you didn't request a password reset, you can safely ignore this email.
          Your password will remain unchanged.
        </p>
 
        <hr style="border: none; border-top: 1px solid #ddd; margin: 20px 0;">
 
        <p style="color: #888; font-size: 12px;">
          If the button doesn't work, copy and paste this link into your browser:<br>
          <a href="${resetUrl}" style="color: #3b82f6; word-break: break-all;">${resetUrl}</a>
        </p>
      </div>
    </body>
    </html>
  `;
 
  return sendEmail({
    to: email,
    subject: "Reset Your Password - Elite Events",
    html});
}
 
/**
 * Send email verification email
 */
export async function sendVerificationEmail(
  email: string,
  token: string
): Promise<boolean> {
  const baseUrl = process.env.NEXTAUTH_URL || "http://localhost:3000";
  const verifyUrl = `${baseUrl}/api/auth/verify-email?token=${token}`;
 
  const html = `
    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Verify Your Email</title>
    </head>
    <body style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; line-height: 1.6; padding: 20px; max-width: 600px; margin: 0 auto;">
      <div style="background-color: #f8f9fa; padding: 20px; border-radius: 8px;">
        <h1 style="color: #333; margin-bottom: 20px;">Welcome to Elite Events!</h1>
 
        <p style="color: #555; margin-bottom: 20px;">
          Thank you for signing up. Please verify your email address by clicking the button below:
        </p>
 
        <a href="${verifyUrl}"
           style="display: inline-block; background-color: #10b981; color: white; padding: 12px 24px; text-decoration: none; border-radius: 6px; font-weight: bold; margin-bottom: 20px;">
          Verify Email Address
        </a>
 
        <p style="color: #666; font-size: 14px; margin-top: 20px;">
          If you didn't create an account with Elite Events, you can safely ignore this email.
        </p>
 
        <hr style="border: none; border-top: 1px solid #ddd; margin: 20px 0;">
 
        <p style="color: #888; font-size: 12px;">
          If the button doesn't work, copy and paste this link into your browser:<br>
          <a href="${verifyUrl}" style="color: #10b981; word-break: break-all;">${verifyUrl}</a>
        </p>
      </div>
    </body>
    </html>
  `;
 
  return sendEmail({
    to: email,
    subject: "Verify Your Email - Elite Events",
    html});
}
 
/**
 * Send account locked notification
 */
export async function sendAccountLockedEmail(
  email: string,
  unlockTime: Date
): Promise<boolean> {
  const unlockTimeStr = unlockTime.toLocaleString();
 
  const html = `
    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Account Security Alert</title>
    </head>
    <body style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; line-height: 1.6; padding: 20px; max-width: 600px; margin: 0 auto;">
      <div style="background-color: #fef2f2; padding: 20px; border-radius: 8px; border: 1px solid #fecaca;">
        <h1 style="color: #dc2626; margin-bottom: 20px;">Account Temporarily Locked</h1>
 
        <p style="color: #555; margin-bottom: 20px;">
          Your Elite Events account has been temporarily locked due to multiple failed login attempts.
        </p>
 
        <p style="color: #555; margin-bottom: 20px;">
          <strong>Your account will be automatically unlocked at:</strong><br>
          ${unlockTimeStr}
        </p>
 
        <p style="color: #666; font-size: 14px;">
          If you didn't attempt to log in, someone else may be trying to access your account.
          We recommend changing your password once you can log in again.
        </p>
 
        <hr style="border: none; border-top: 1px solid #fecaca; margin: 20px 0;">
 
        <p style="color: #888; font-size: 12px;">
          If you need immediate assistance, please contact our support team.
        </p>
      </div>
    </body>
    </html>
  `;
 
  return sendEmail({
    to: email,
    subject: "Account Security Alert - Elite Events",
    html});
}
 
// ============================================================================
// PROMOTION EMAIL TEMPLATES
// ============================================================================
 
interface PromotionEmailData {
  promotionName: string;
  description?: string;
  discountText: string;
  promoCode?: string;
  startDate?: Date;
  endDate?: Date;
  ctaUrl?: string;
}
 
/**
 * Send promotion announcement email
 */
export async function sendPromotionAnnouncementEmail(
  email: string,
  userName: string,
  promotion: PromotionEmailData
): Promise<boolean> {
  const baseUrl = process.env.NEXTAUTH_URL || "http://localhost:3000";
  const ctaUrl = promotion.ctaUrl || `${baseUrl}/promotions`;
  const endDateStr = promotion.endDate
    ? promotion.endDate.toLocaleDateString("en-US", {
        month: "long",
        day: "numeric",
        year: "numeric"})
    : null;
 
  const html = `
    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>New Promotion Alert!</title>
    </head>
    <body style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; line-height: 1.6; padding: 20px; max-width: 600px; margin: 0 auto; background-color: #f9fafb;">
      <div style="background-color: #ffffff; padding: 30px; border-radius: 12px; box-shadow: 0 2px 8px rgba(0,0,0,0.05);">
        <div style="text-align: center; margin-bottom: 24px;">
          <span style="background: linear-gradient(135deg, #3b82f6, #8b5cf6); color: white; padding: 8px 16px; border-radius: 20px; font-size: 12px; font-weight: bold; text-transform: uppercase; letter-spacing: 1px;">
            New Promotion
          </span>
        </div>
 
        <h1 style="color: #1f2937; margin-bottom: 16px; text-align: center; font-size: 28px;">
          ${promotion.promotionName}
        </h1>
 
        <div style="background: linear-gradient(135deg, #10b981, #059669); color: white; padding: 24px; border-radius: 12px; text-align: center; margin-bottom: 24px;">
          <p style="font-size: 36px; font-weight: bold; margin: 0;">
            ${promotion.discountText}
          </p>
          ${promotion.description ? `<p style="font-size: 14px; margin-top: 8px; opacity: 0.9;">${promotion.description}</p>` : ""}
        </div>
 
        <p style="color: #4b5563; margin-bottom: 20px;">
          Hi ${userName},
        </p>
 
        <p style="color: #4b5563; margin-bottom: 20px;">
          Great news! We have a special promotion just for you. Don't miss this opportunity to save on your favorite products.
        </p>
 
        ${
          promotion.promoCode
            ? `
        <div style="background-color: #f3f4f6; border: 2px dashed #d1d5db; padding: 16px; border-radius: 8px; text-align: center; margin-bottom: 24px;">
          <p style="color: #6b7280; font-size: 12px; margin: 0 0 8px 0; text-transform: uppercase;">Use Code</p>
          <p style="font-size: 24px; font-weight: bold; color: #1f2937; margin: 0; font-family: monospace; letter-spacing: 2px;">
            ${promotion.promoCode}
          </p>
        </div>
        `
            : ""
        }
 
        ${
          endDateStr
            ? `
        <p style="color: #dc2626; font-size: 14px; text-align: center; margin-bottom: 24px;">
          <strong>Hurry! Offer ends ${endDateStr}</strong>
        </p>
        `
            : ""
        }
 
        <div style="text-align: center;">
          <a href="${ctaUrl}"
             style="display: inline-block; background: linear-gradient(135deg, #3b82f6, #2563eb); color: white; padding: 14px 32px; text-decoration: none; border-radius: 8px; font-weight: bold; font-size: 16px;">
            Shop Now
          </a>
        </div>
 
        <hr style="border: none; border-top: 1px solid #e5e7eb; margin: 30px 0;">
 
        <p style="color: #9ca3af; font-size: 12px; text-align: center;">
          You received this email because you opted in to promotional emails from Elite Events.<br>
          <a href="${baseUrl}/account/settings" style="color: #6b7280;">Manage email preferences</a>
        </p>
      </div>
    </body>
    </html>
  `;
 
  return sendEmail({
    to: email,
    subject: `${promotion.discountText} - ${promotion.promotionName} | Elite Events`,
    html});
}
 
/**
 * Send flash sale alert email
 */
export async function sendFlashSaleAlertEmail(
  email: string,
  userName: string,
  sale: {
    name: string;
    discountText: string;
    startTime: Date;
    endTime: Date;
    headline?: string;
  }
): Promise<boolean> {
  const baseUrl = process.env.NEXTAUTH_URL || "http://localhost:3000";
  const startTimeStr = sale.startTime.toLocaleString("en-US", {
    weekday: "short",
    month: "short",
    day: "numeric",
    hour: "numeric",
    minute: "2-digit"});
  const endTimeStr = sale.endTime.toLocaleString("en-US", {
    weekday: "short",
    month: "short",
    day: "numeric",
    hour: "numeric",
    minute: "2-digit"});
 
  const html = `
    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Flash Sale Alert!</title>
    </head>
    <body style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; line-height: 1.6; padding: 20px; max-width: 600px; margin: 0 auto; background-color: #1f2937;">
      <div style="background: linear-gradient(135deg, #dc2626, #b91c1c); padding: 30px; border-radius: 12px; text-align: center;">
        <div style="animation: pulse 2s infinite;">
          <span style="font-size: 48px;">⚡</span>
        </div>
 
        <h1 style="color: white; margin: 16px 0; font-size: 32px; text-transform: uppercase; letter-spacing: 2px;">
          Flash Sale
        </h1>
 
        ${sale.headline ? `<p style="color: #fecaca; font-size: 18px; margin-bottom: 16px;">${sale.headline}</p>` : ""}
 
        <div style="background-color: rgba(255,255,255,0.15); padding: 24px; border-radius: 12px; margin: 24px 0;">
          <p style="color: white; font-size: 48px; font-weight: bold; margin: 0;">
            ${sale.discountText}
          </p>
          <p style="color: #fecaca; font-size: 16px; margin-top: 8px;">
            ${sale.name}
          </p>
        </div>
 
        <div style="background-color: rgba(0,0,0,0.2); padding: 16px; border-radius: 8px; margin-bottom: 24px;">
          <p style="color: #fecaca; font-size: 12px; margin: 0 0 4px 0; text-transform: uppercase;">Sale Period</p>
          <p style="color: white; font-size: 14px; margin: 0;">
            ${startTimeStr} - ${endTimeStr}
          </p>
        </div>
 
        <p style="color: #fecaca; margin-bottom: 24px;">
          Hi ${userName}, this is your exclusive early alert!
        </p>
 
        <a href="${baseUrl}/promotions"
           style="display: inline-block; background-color: white; color: #dc2626; padding: 16px 40px; text-decoration: none; border-radius: 8px; font-weight: bold; font-size: 18px; text-transform: uppercase;">
          Shop Flash Sale
        </a>
 
        <p style="color: #fca5a5; font-size: 12px; margin-top: 24px;">
          Limited time only. While supplies last.
        </p>
      </div>
 
      <p style="color: #6b7280; font-size: 11px; text-align: center; margin-top: 20px;">
        <a href="${baseUrl}/account/settings" style="color: #6b7280;">Unsubscribe from flash sale alerts</a>
      </p>
    </body>
    </html>
  `;
 
  return sendEmail({
    to: email,
    subject: `⚡ FLASH SALE: ${sale.discountText} - Starts ${startTimeStr}!`,
    html});
}
 
/**
 * Send expiring promotion reminder email
 */
export async function sendExpiringPromoReminderEmail(
  email: string,
  userName: string,
  promotion: {
    name: string;
    discountText: string;
    promoCode?: string;
    expiresAt: Date;
    hoursRemaining: number;
  }
): Promise<boolean> {
  const baseUrl = process.env.NEXTAUTH_URL || "http://localhost:3000";
  const expiresStr = promotion.expiresAt.toLocaleString("en-US", {
    weekday: "long",
    month: "long",
    day: "numeric",
    hour: "numeric",
    minute: "2-digit"});
 
  const urgencyColor =
    promotion.hoursRemaining <= 6
      ? "#dc2626"
      : promotion.hoursRemaining <= 24
        ? "#f59e0b"
        : "#3b82f6";
 
  const html = `
    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Your Promotion is Expiring!</title>
    </head>
    <body style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; line-height: 1.6; padding: 20px; max-width: 600px; margin: 0 auto; background-color: #f9fafb;">
      <div style="background-color: #ffffff; padding: 30px; border-radius: 12px; border-top: 4px solid ${urgencyColor};">
        <div style="text-align: center; margin-bottom: 24px;">
          <span style="font-size: 48px;">⏰</span>
        </div>
 
        <h1 style="color: #1f2937; margin-bottom: 8px; text-align: center; font-size: 24px;">
          Don't Miss Out!
        </h1>
 
        <p style="color: ${urgencyColor}; font-weight: bold; text-align: center; font-size: 18px; margin-bottom: 24px;">
          Only ${promotion.hoursRemaining} hour${promotion.hoursRemaining !== 1 ? "s" : ""} left!
        </p>
 
        <p style="color: #4b5563; margin-bottom: 20px;">
          Hi ${userName},
        </p>
 
        <p style="color: #4b5563; margin-bottom: 20px;">
          Your promotion <strong>${promotion.name}</strong> is about to expire. Use it before it's gone!
        </p>
 
        <div style="background-color: #fef3c7; border: 1px solid #fcd34d; padding: 20px; border-radius: 8px; text-align: center; margin-bottom: 24px;">
          <p style="font-size: 32px; font-weight: bold; color: #92400e; margin: 0;">
            ${promotion.discountText}
          </p>
          ${
            promotion.promoCode
              ? `
          <p style="color: #78716c; font-size: 12px; margin-top: 12px; margin-bottom: 8px;">Use Code:</p>
          <p style="font-size: 20px; font-weight: bold; color: #1f2937; margin: 0; font-family: monospace;">
            ${promotion.promoCode}
          </p>
          `
              : ""
          }
        </div>
 
        <p style="color: #6b7280; font-size: 14px; text-align: center; margin-bottom: 24px;">
          Expires: ${expiresStr}
        </p>
 
        <div style="text-align: center;">
          <a href="${baseUrl}/shop"
             style="display: inline-block; background-color: ${urgencyColor}; color: white; padding: 14px 32px; text-decoration: none; border-radius: 8px; font-weight: bold; font-size: 16px;">
            Use My Discount Now
          </a>
        </div>
 
        <hr style="border: none; border-top: 1px solid #e5e7eb; margin: 30px 0;">
 
        <p style="color: #9ca3af; font-size: 12px; text-align: center;">
          <a href="${baseUrl}/account/settings" style="color: #6b7280;">Manage email preferences</a>
        </p>
      </div>
    </body>
    </html>
  `;
 
  return sendEmail({
    to: email,
    subject: `⏰ ${promotion.hoursRemaining}h left! Your ${promotion.discountText} expires soon`,
    html});
}
 
// ============================================================================
// LOYALTY PROGRAM EMAIL TEMPLATES
// ============================================================================
 
/**
 * Send points earned confirmation email
 */
export async function sendLoyaltyPointsEarnedEmail(
  email: string,
  userName: string,
  data: {
    pointsEarned: number;
    orderTotal: number;
    newBalance: number;
    tierName?: string;
    nextTierName?: string;
    pointsToNextTier?: number;
  }
): Promise<boolean> {
  const baseUrl = process.env.NEXTAUTH_URL || "http://localhost:3000";
 
  const formatCurrency = (amount: number) =>
    new Intl.NumberFormat("en-US", {
      style: "currency",
      currency: "USD"}).format(amount);
 
  const formatPoints = (points: number) =>
    new Intl.NumberFormat("en-US").format(points);
 
  const html = `
    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>You Earned Loyalty Points!</title>
    </head>
    <body style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; line-height: 1.6; padding: 20px; max-width: 600px; margin: 0 auto; background-color: #f9fafb;">
      <div style="background-color: #ffffff; padding: 30px; border-radius: 12px;">
        <div style="text-align: center; margin-bottom: 24px;">
          <span style="font-size: 48px;">⭐</span>
        </div>
 
        <h1 style="color: #1f2937; margin-bottom: 8px; text-align: center; font-size: 24px;">
          Points Earned!
        </h1>
 
        <p style="color: #6b7280; text-align: center; margin-bottom: 24px;">
          Thank you for your purchase, ${userName}!
        </p>
 
        <div style="background: linear-gradient(135deg, #fbbf24, #f59e0b); padding: 24px; border-radius: 12px; text-align: center; margin-bottom: 24px;">
          <p style="color: rgba(255,255,255,0.8); font-size: 14px; margin: 0;">You earned</p>
          <p style="color: white; font-size: 48px; font-weight: bold; margin: 8px 0;">
            +${formatPoints(data.pointsEarned)}
          </p>
          <p style="color: rgba(255,255,255,0.8); font-size: 14px; margin: 0;">points</p>
        </div>
 
        <div style="background-color: #f3f4f6; padding: 20px; border-radius: 8px; margin-bottom: 24px;">
          <div style="display: flex; justify-content: space-between; margin-bottom: 12px;">
            <span style="color: #6b7280;">Order Total</span>
            <span style="color: #1f2937; font-weight: bold;">${formatCurrency(data.orderTotal)}</span>
          </div>
          <div style="display: flex; justify-content: space-between; margin-bottom: 12px;">
            <span style="color: #6b7280;">Points Earned</span>
            <span style="color: #f59e0b; font-weight: bold;">+${formatPoints(data.pointsEarned)}</span>
          </div>
          <hr style="border: none; border-top: 1px solid #d1d5db; margin: 12px 0;">
          <div style="display: flex; justify-content: space-between;">
            <span style="color: #1f2937; font-weight: bold;">New Balance</span>
            <span style="color: #1f2937; font-weight: bold; font-size: 18px;">${formatPoints(data.newBalance)} pts</span>
          </div>
        </div>
 
        ${
          data.tierName
            ? `
        <div style="text-align: center; margin-bottom: 24px;">
          <span style="background-color: #dbeafe; color: #1d4ed8; padding: 6px 16px; border-radius: 20px; font-size: 14px; font-weight: bold;">
            ${data.tierName} Member
          </span>
        </div>
        `
            : ""
        }
 
        ${
          data.nextTierName && data.pointsToNextTier
            ? `
        <div style="background-color: #f0fdf4; border: 1px solid #bbf7d0; padding: 16px; border-radius: 8px; text-align: center; margin-bottom: 24px;">
          <p style="color: #166534; font-size: 14px; margin: 0;">
            Only <strong>${formatPoints(data.pointsToNextTier)}</strong> points to reach <strong>${data.nextTierName}</strong> status!
          </p>
        </div>
        `
            : ""
        }
 
        <div style="text-align: center;">
          <a href="${baseUrl}/account/loyalty"
             style="display: inline-block; background-color: #f59e0b; color: white; padding: 14px 32px; text-decoration: none; border-radius: 8px; font-weight: bold; font-size: 16px;">
            View My Rewards
          </a>
        </div>
 
        <hr style="border: none; border-top: 1px solid #e5e7eb; margin: 30px 0;">
 
        <p style="color: #9ca3af; font-size: 12px; text-align: center;">
          <a href="${baseUrl}/account/settings" style="color: #6b7280;">Manage email preferences</a>
        </p>
      </div>
    </body>
    </html>
  `;
 
  return sendEmail({
    to: email,
    subject: `⭐ You earned ${formatPoints(data.pointsEarned)} points! | Elite Events`,
    html});
}
 
/**
 * Send loyalty tier upgrade notification email
 */
export async function sendLoyaltyTierUpgradeEmail(
  email: string,
  userName: string,
  data: {
    previousTier: string;
    newTier: string;
    perks: string[];
    totalPoints: number;
  }
): Promise<boolean> {
  const baseUrl = process.env.NEXTAUTH_URL || "http://localhost:3000";
 
  const formatPoints = (points: number) =>
    new Intl.NumberFormat("en-US").format(points);
 
  const tierColors: Record<string, { bg: string; text: string }> = {
    Bronze: { bg: "#f59e0b", text: "#78350f" },
    Silver: { bg: "#94a3b8", text: "#1e293b" },
    Gold: { bg: "#fbbf24", text: "#713f12" },
    Platinum: { bg: "#a78bfa", text: "#4c1d95" }};
 
  const color = tierColors[data.newTier] || { bg: "#3b82f6", text: "#1e3a8a" };
 
  const html = `
    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Congratulations! You've Been Upgraded!</title>
    </head>
    <body style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; line-height: 1.6; padding: 20px; max-width: 600px; margin: 0 auto; background-color: #f9fafb;">
      <div style="background-color: #ffffff; padding: 30px; border-radius: 12px;">
        <div style="text-align: center; margin-bottom: 24px;">
          <span style="font-size: 48px;">🎉</span>
        </div>
 
        <h1 style="color: #1f2937; margin-bottom: 8px; text-align: center; font-size: 28px;">
          Congratulations, ${userName}!
        </h1>
 
        <p style="color: #6b7280; text-align: center; margin-bottom: 24px; font-size: 18px;">
          You've been upgraded to
        </p>
 
        <div style="background-color: ${color.bg}; padding: 32px; border-radius: 16px; text-align: center; margin-bottom: 24px;">
          <p style="color: ${color.text}; font-size: 36px; font-weight: bold; margin: 0; text-transform: uppercase; letter-spacing: 2px;">
            ${data.newTier}
          </p>
          <p style="color: ${color.text}; opacity: 0.8; font-size: 14px; margin-top: 8px;">
            Member Status
          </p>
        </div>
 
        <p style="color: #4b5563; text-align: center; margin-bottom: 24px;">
          Your loyalty has earned you exclusive ${data.newTier} benefits!
        </p>
 
        <div style="background-color: #f3f4f6; padding: 20px; border-radius: 8px; margin-bottom: 24px;">
          <p style="color: #1f2937; font-weight: bold; margin: 0 0 12px 0;">Your New Perks:</p>
          <ul style="color: #4b5563; margin: 0; padding-left: 20px;">
            ${data.perks.map((perk) => `<li style="margin-bottom: 8px;">${perk}</li>`).join("")}
          </ul>
        </div>
 
        <div style="text-align: center; margin-bottom: 24px;">
          <p style="color: #6b7280; font-size: 14px; margin-bottom: 4px;">Total Points</p>
          <p style="color: #1f2937; font-size: 24px; font-weight: bold; margin: 0;">
            ${formatPoints(data.totalPoints)} pts
          </p>
        </div>
 
        <div style="text-align: center;">
          <a href="${baseUrl}/account/loyalty"
             style="display: inline-block; background-color: ${color.bg}; color: ${color.text}; padding: 14px 32px; text-decoration: none; border-radius: 8px; font-weight: bold; font-size: 16px;">
            View My Benefits
          </a>
        </div>
 
        <hr style="border: none; border-top: 1px solid #e5e7eb; margin: 30px 0;">
 
        <p style="color: #9ca3af; font-size: 12px; text-align: center;">
          <a href="${baseUrl}/account/settings" style="color: #6b7280;">Manage email preferences</a>
        </p>
      </div>
    </body>
    </html>
  `;
 
  return sendEmail({
    to: email,
    subject: `🎉 You're Now a ${data.newTier} Member! | Elite Events`,
    html});
}
 
// ============================================================================
// REFERRAL PROGRAM EMAIL TEMPLATES
// ============================================================================
 
/**
 * Send referral reward notification email
 */
export async function sendReferralRewardEmail(
  email: string,
  userName: string,
  data: {
    refereeName: string;
    rewardType: "points" | "credit" | "discount";
    rewardValue: number;
    totalReferrals: number;
  }
): Promise<boolean> {
  const baseUrl = process.env.NEXTAUTH_URL || "http://localhost:3000";
 
  const formatReward = () => {
    switch (data.rewardType) {
      case "points":
        return `${new Intl.NumberFormat("en-US").format(data.rewardValue)} points`;
      case "credit":
        return new Intl.NumberFormat("en-US", {
          style: "currency",
          currency: "USD"}).format(data.rewardValue);
      case "discount":
        return `${data.rewardValue}% off`;
    }
  };
 
  const html = `
    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>You Earned a Referral Reward!</title>
    </head>
    <body style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; line-height: 1.6; padding: 20px; max-width: 600px; margin: 0 auto; background-color: #f9fafb;">
      <div style="background-color: #ffffff; padding: 30px; border-radius: 12px;">
        <div style="text-align: center; margin-bottom: 24px;">
          <span style="font-size: 48px;">🎁</span>
        </div>
 
        <h1 style="color: #1f2937; margin-bottom: 8px; text-align: center; font-size: 24px;">
          Your Referral Paid Off!
        </h1>
 
        <p style="color: #6b7280; text-align: center; margin-bottom: 24px;">
          Great news, ${userName}!
        </p>
 
        <div style="background: linear-gradient(135deg, #8b5cf6, #6366f1); padding: 24px; border-radius: 12px; text-align: center; margin-bottom: 24px;">
          <p style="color: rgba(255,255,255,0.8); font-size: 14px; margin: 0;">Your friend <strong>${data.refereeName}</strong> made a purchase!</p>
          <p style="color: white; font-size: 36px; font-weight: bold; margin: 16px 0;">
            +${formatReward()}
          </p>
          <p style="color: rgba(255,255,255,0.8); font-size: 14px; margin: 0;">has been added to your account</p>
        </div>
 
        <div style="background-color: #f3f4f6; padding: 16px; border-radius: 8px; text-align: center; margin-bottom: 24px;">
          <p style="color: #6b7280; font-size: 14px; margin: 0;">
            Total Successful Referrals: <strong style="color: #1f2937;">${data.totalReferrals}</strong>
          </p>
        </div>
 
        <p style="color: #4b5563; text-align: center; margin-bottom: 24px;">
          Keep sharing your referral code to earn more rewards!
        </p>
 
        <div style="text-align: center;">
          <a href="${baseUrl}/account/referrals"
             style="display: inline-block; background: linear-gradient(135deg, #8b5cf6, #6366f1); color: white; padding: 14px 32px; text-decoration: none; border-radius: 8px; font-weight: bold; font-size: 16px;">
            Invite More Friends
          </a>
        </div>
 
        <hr style="border: none; border-top: 1px solid #e5e7eb; margin: 30px 0;">
 
        <p style="color: #9ca3af; font-size: 12px; text-align: center;">
          <a href="${baseUrl}/account/settings" style="color: #6b7280;">Manage email preferences</a>
        </p>
      </div>
    </body>
    </html>
  `;
 
  return sendEmail({
    to: email,
    subject: `🎁 You earned ${formatReward()} from your referral!`,
    html});
}
 
/**
 * Send birthday offer email
 */
export async function sendBirthdayOfferEmail(
  email: string,
  userName: string,
  data: {
    discountPercent: number;
    promoCode: string;
    expiresAt: Date;
  }
): Promise<boolean> {
  const baseUrl = process.env.NEXTAUTH_URL || "http://localhost:3000";
  const expiresStr = data.expiresAt.toLocaleDateString("en-US", {
    month: "long",
    day: "numeric"});
 
  const html = `
    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Happy Birthday!</title>
    </head>
    <body style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; line-height: 1.6; padding: 20px; max-width: 600px; margin: 0 auto; background-color: #fdf2f8;">
      <div style="background-color: #ffffff; padding: 30px; border-radius: 12px;">
        <div style="text-align: center; margin-bottom: 24px;">
          <span style="font-size: 64px;">🎂</span>
        </div>
 
        <h1 style="color: #1f2937; margin-bottom: 8px; text-align: center; font-size: 32px;">
          Happy Birthday, ${userName}!
        </h1>
 
        <p style="color: #6b7280; text-align: center; margin-bottom: 24px; font-size: 18px;">
          We have a special gift for you! 🎁
        </p>
 
        <div style="background: linear-gradient(135deg, #ec4899, #f472b6); padding: 32px; border-radius: 16px; text-align: center; margin-bottom: 24px;">
          <p style="color: rgba(255,255,255,0.9); font-size: 16px; margin: 0;">Enjoy</p>
          <p style="color: white; font-size: 56px; font-weight: bold; margin: 8px 0;">
            ${data.discountPercent}% OFF
          </p>
          <p style="color: rgba(255,255,255,0.9); font-size: 16px; margin: 0;">your entire order</p>
        </div>
 
        <div style="background-color: #fdf2f8; border: 2px dashed #f9a8d4; padding: 20px; border-radius: 8px; text-align: center; margin-bottom: 24px;">
          <p style="color: #9d174d; font-size: 12px; margin: 0 0 8px 0; text-transform: uppercase;">Your Birthday Code</p>
          <p style="font-size: 28px; font-weight: bold; color: #be185d; margin: 0; font-family: monospace; letter-spacing: 3px;">
            ${data.promoCode}
          </p>
        </div>
 
        <p style="color: #6b7280; font-size: 14px; text-align: center; margin-bottom: 24px;">
          Valid until ${expiresStr}
        </p>
 
        <div style="text-align: center;">
          <a href="${baseUrl}/shop"
             style="display: inline-block; background: linear-gradient(135deg, #ec4899, #db2777); color: white; padding: 16px 40px; text-decoration: none; border-radius: 8px; font-weight: bold; font-size: 18px;">
            Treat Yourself 🎉
          </a>
        </div>
 
        <hr style="border: none; border-top: 1px solid #f9a8d4; margin: 30px 0;">
 
        <p style="color: #9ca3af; font-size: 12px; text-align: center;">
          Wishing you a wonderful birthday from all of us at Elite Events!
        </p>
      </div>
    </body>
    </html>
  `;
 
  return sendEmail({
    to: email,
    subject: `🎂 Happy Birthday ${userName}! Here's ${data.discountPercent}% OFF just for you!`,
    html});
}