import { NextRequest, NextResponse } from 'next/server';
import { generateOptInPage, generateSlug } from '@/lib/landing-page-generator';
import type { LandingPageConfig } from '@/lib/landing-page-generator';

export async function POST(req: NextRequest) {
  try {
    const body = await req.json();

    const config: LandingPageConfig = {
      businessName: body.businessName || 'Your Business',
      businessSlug: body.businessSlug || generateSlug(body.businessName || 'your-business'),
      useCase: body.useCase || 'SMS notifications and updates',
      messageFrequency: body.messageFrequency || 'up to 5 messages per week',
      contactEmail: body.contactEmail || 'contact@example.com',
      contactPhone: body.contactPhone || '+15551234567',
      brandColor: body.brandColor || '#3B82F6',
      logoUrl: body.logoUrl || undefined,
    };

    const html = generateOptInPage(config);
    return new NextResponse(html, {
      headers: { 'Content-Type': 'text/html; charset=utf-8' },
    });
  } catch (err: unknown) {
    const message = err instanceof Error ? err.message : 'Unknown error';
    return NextResponse.json({ error: message }, { status: 500 });
  }
}
