import { NextRequest, NextResponse } from 'next/server';
import { getLandingPage } from '@/lib/db';

export async function GET(
  _req: NextRequest,
  { params }: { params: { id: string } }
) {
  try {
    const page = getLandingPage(params.id);
    if (!page) {
      return NextResponse.json({ error: 'Not found' }, { status: 404 });
    }

    // Build a simple ZIP-like downloadable bundle
    // We return a JSON manifest that the client can use to download individual files
    // For actual ZIP support, the client-side will handle it with JSZip
    return NextResponse.json({
      slug: page.business_slug,
      files: [
        { name: 'index.html', content: page.opt_in_html },
        { name: 'privacy-policy.html', content: page.privacy_html },
        { name: 'terms.html', content: page.terms_html },
      ],
    });
  } catch (err: unknown) {
    const message = err instanceof Error ? err.message : 'Unknown error';
    return NextResponse.json({ error: message }, { status: 500 });
  }
}
