import { NextRequest, NextResponse } from 'next/server'; export async function POST(req: NextRequest) { try { const { campaignName, subject, htmlContent, scheduledAt } = await req.json(); if (!campaignName || !subject || !htmlContent) { return NextResponse.json( { error: 'Chýbajú povinné polia (campaignName, subject, htmlContent).' }, { status: 400 } ); } const apiKey = process.env.BREVO_API_KEY; const senderName = process.env.BREVO_SENDER_NAME || 'Jandl'; const senderEmail = process.env.BREVO_SENDER_EMAIL || 'tomas.rafay@jandl.sk'; if (!apiKey) { return NextResponse.json( { error: 'BREVO_API_KEY nie je nakonfigurovaný na serveri.' }, { status: 500 } ); } // Volanie Brevo API v3 const payload: any = { name: campaignName, subject: subject, sender: { name: senderName, email: senderEmail }, type: 'classic', htmlContent: htmlContent }; if (scheduledAt) { payload.scheduledAt = scheduledAt; } const brevoRes = await fetch('https://api.brevo.com/v3/emailCampaigns', { method: 'POST', headers: { 'accept': 'application/json', 'content-type': 'application/json', 'api-key': apiKey }, body: JSON.stringify(payload) }); const data = await brevoRes.json(); if (!brevoRes.ok) { console.error('Brevo API error:', data); return NextResponse.json( { error: data.message || 'Chyba pri vytváraní kampane v Brevo', details: data }, { status: brevoRes.status } ); } // Úspech - vrátime campaignId a URL na správu v Brevo return NextResponse.json({ success: true, campaignId: data.id, brevoUrl: `https://app.brevo.com/camp/detail/email/${data.id}`, message: 'Kampaň bola úspešne vytvorená v Brevo!' }); } catch (error: any) { console.error('Export to Brevo error:', error); return NextResponse.json( { error: error.message || 'Interná chyba servera' }, { status: 500 } ); } }