import { NextRequest, NextResponse } from 'next/server' import nodemailer from 'nodemailer' import { createClient } from '@supabase/supabase-js' 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 supabase = createClient( process.env.NEXT_PUBLIC_SUPABASE_URL || '', process.env.SUPABASE_SERVICE_ROLE_KEY || '' ) const body = await request.json() const { title, content, actionUrl, actionText } = body if (!title || !content) { return NextResponse.json( { error: 'Title and content are required' }, { status: 400 } ) } // Get all users who opted in to email updates const { data: preferences, error: prefsError } = await supabase .from('notification_preferences') .select('user_id') .eq('email_updates', true) if (prefsError) { console.error('Error fetching preferences:', prefsError) return NextResponse.json( { error: 'Failed to fetch preferences' }, { status: 500 } ) } if (!preferences || preferences.length === 0) { return NextResponse.json( { success: true, message: 'No users with email updates enabled', sent: 0, }, { status: 200 } ) } const userIds = preferences.map((p: any) => p.user_id) // Get user emails const { data: users, error: usersError } = await supabase .from('users') .select('user_id, user_name, email') .in('user_id', userIds) if (usersError) { console.error('Error fetching users:', usersError) return NextResponse.json( { error: 'Failed to fetch users' }, { status: 500 } ) } if (!users || users.length === 0) { return NextResponse.json( { success: true, message: 'No users with email updates enabled', sent: 0, }, { status: 200 } ) } const transporter = createTransporter() let successCount = 0 let failureCount = 0 // Send email to each user for (const user of users) { try { const emailHtml = `

BUILDMATE

📢 General Update

Hello ${user.user_name},
${content}
${actionUrl && actionText ? `
${actionText}
` : ''}
💡 Update Tips
Check your BUILDMATE account regularly for the latest features, improvements, and announcements. You can manage your email preferences in Settings anytime.
` await transporter.sendMail({ from: process.env.SMTP_FROM_EMAIL || 'BUILDMATE ', to: user.email, subject: title, html: emailHtml, }) successCount++ console.log(`✅ General update email sent to ${user.email}`) } catch (emailError) { failureCount++ console.error(`❌ Failed to send email to ${user.email}:`, emailError) } } return NextResponse.json( { success: true, message: `General update emails sent to ${successCount} users (${failureCount} failures)`, sent: successCount, failed: failureCount, }, { status: 200 } ) } catch (error) { console.error('Send general update error:', error) return NextResponse.json( { error: error instanceof Error ? error.message : 'Failed to send general update' }, { status: 500 } ) } }