import { NextRequest, NextResponse } from "next/server"; export async function POST(req: NextRequest) { try { const body = await req.json() as { query: string; region: string; count: number }; const { query, region, count } = body; if (!query || typeof query !== "string") { return NextResponse.json({ error: "Suchbegriff fehlt" }, { status: 400 }); } const searchQuery = region ? `${query} ${region}` : query; const base = `http://localhost:${process.env.PORT || 3000}`; // ── 1. Maps job (always, max 60) ────────────────────────────────────────── const mapsRes = await fetch(`${base}/api/jobs/maps-enrich`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ queries: [searchQuery], maxResultsPerQuery: Math.min(count, 60), languageCode: "de", categories: ["ceo"], enrichEmails: true, }), }); if (!mapsRes.ok) { const err = await mapsRes.json() as { error?: string }; return NextResponse.json({ error: err.error || "Suche konnte nicht gestartet werden" }, { status: 500 }); } const { jobId } = await mapsRes.json() as { jobId: string }; return NextResponse.json({ jobId }); } catch (err) { console.error("POST /api/search error:", err); return NextResponse.json({ error: "Suche konnte nicht gestartet werden" }, { status: 500 }); } }