import { NextRequest, NextResponse } from 'next/server' import nodemailer from 'nodemailer' export const dynamic = 'force-dynamic' export const runtime = 'nodejs' const createTransporter = () => { const smtpHost = process.env.SMTP_HOST const smtpPort = process.env.SMTP_PORT ? parseInt(process.env.SMTP_PORT, 10) : 587 const smtpUser = process.env.SMTP_USER?.trim() const smtpPassword = (process.env.SMTP_PASSWORD || '').replace(/\s/g, '').trim() const smtpFromEmail = process.env.SMTP_FROM_EMAIL || 'BUILDMATE ' const isPlaceholder = (smtpHost?.trim() === 'your_smtp_host') || (smtpUser === 'your_smtp_user') || (smtpPassword === 'your_smtp_password') if (isPlaceholder) { throw new Error('SMTP credentials are still placeholders. Please update .env.local.') } if (!smtpHost || !smtpUser || !smtpPassword) { throw new Error('SMTP credentials are missing in environment variables.') } const isGmail = smtpHost.toLowerCase().includes('gmail.com') const port = smtpPort || 587 const secure = port === 465 return nodemailer.createTransport({ host: smtpHost, port, secure, ...(isGmail && { requireTLS: true }), auth: { user: smtpUser, pass: smtpPassword }, }) } export async function POST(request: NextRequest) { try { const body = await request.json() const { email, username } = body if (!email || !username) { return NextResponse.json( { error: 'Email and username are required' }, { status: 400 } ) } const transporter = createTransporter() const appUrl = process.env.NEXT_PUBLIC_APP_URL || 'https://buildmate.com' const emailHtml = `

BUILDMATE

🎉 Welcome to the Community

Hello ${username},
Welcome to BUILDMATE! We're thrilled to have you join our community of PC builders. Your account has been created successfully, and you're all set to start building your perfect PC.
💾Save Your Builds
Create and save multiple PC builds with different configurations. Compare specs, prices, and performance all in one place.
🔍Smart Filtering
Filter components by price, performance, brand, and more. Our advanced algorithm helps you find the perfect components for your budget.
👥Share & Explore
Share your builds with friends and discover builds from our community. Get inspired by other builders.
📊Build Analytics
See performance metrics, compatibility checks, and detailed specifications for each component.
Start Building Now
📚 Getting Started
New to PC building? Check out our Build Guide to learn about choosing components, understanding specifications, and assembling your PC.

If you have any questions or need help getting started, feel free to reach out to our support team. Happy building!

` await transporter.sendMail({ from: process.env.SMTP_FROM_EMAIL || 'BUILDMATE ', to: email, subject: '🎉 Welcome to BUILDMATE - Let\'s Get Started!', html: emailHtml, }) console.log(`✅ Welcome email sent to ${email}`) return NextResponse.json( { success: true, message: 'Welcome email sent successfully', }, { status: 200 } ) } catch (error) { console.error('Send welcome email error:', error) return NextResponse.json( { error: error instanceof Error ? error.message : 'Failed to send welcome email' }, { status: 500 } ) } }