// Auteur : Simon-Pierre Boucher — contact@spboucher.ai import { NextRequest, NextResponse } from "next/server"; import { estimateByUnitId, indexType } from "@/lib/estimator"; import { getMarketIndex } from "@/lib/db"; import { buildProReport } from "@/lib/report-pro"; export async function GET(req: NextRequest) { const rawId = req.nextUrl.searchParams.get("id"); if (!rawId) return NextResponse.json({ error: "id requis" }, { status: 400 }); const id = rawId.replace(/\D/g, "") || rawId; const data = estimateByUnitId(id); if (!data || !data.unit) return NextResponse.json({ error: "unité introuvable" }, { status: 404 }); const idx = getMarketIndex(indexType(data.unit.typeProp)); const pdf = await buildProReport(data, idx); const slug = (data.unit.adresse ?? "propriete") .toLowerCase() .normalize("NFD") .replace(/[̀-ͯ]/g, "") .replace(/[^a-z0-9]+/g, "-") .replace(/^-+|-+$/g, ""); return new NextResponse(new Uint8Array(pdf), { headers: { "Content-Type": "application/pdf", "Content-Disposition": `attachment; filename="vrai-prix-rapport-detaille-${slug}.pdf"`, "Cache-Control": "private, max-age=300", }, }); }