import { NextRequest, NextResponse } from "next/server"; import { getDb } from "@/lib/db"; import type { ListingRow, ListingCard } from "@/lib/types"; import { directCardsByUids } from "@/lib/listings/public"; type Row = ListingRow & { estimate: number | null; ratio: number | null; verdict: string | null; confidence: string | null }; export const dynamic = "force-dynamic"; export async function GET(req: NextRequest) { const uids = (req.nextUrl.searchParams.get("uids") ?? "").split(",").filter(Boolean).slice(0, 50); if (!uids.length) return NextResponse.json({ items: [] }); try { const agg = uids.filter((u) => !u.startsWith("toveli:")); const rows = agg.length ? getDb().prepare(`SELECT l.*, v.estimate, v.ratio, v.verdict, v.confidence FROM listing l LEFT JOIN valuation v ON v.listing_uid = l.uid WHERE l.uid IN (${agg.map(() => "?").join(",")})`).all(...agg) as Row[] : []; const items: ListingCard[] = rows.map((r) => { let image: string | null = null; try { const a = JSON.parse(r.images ?? "[]"); image = a[0] ?? null; } catch { /* */ } return { uid: r.uid, propertyId: r.property_id, market: r.market, price: r.price, address: r.address, city: r.city, region: r.region, category: r.category, typeLabel: r.type_label, bedrooms: r.bedrooms, bathrooms: r.bathrooms, areaSqft: r.area_sqft, lat: r.lat, lng: r.lng, image, imageCount: r.image_count ?? 0, connector: r.connector, sourceId: r.source_id, url: r.url, firstSeen: r.first_seen, estimate: r.estimate, ratio: r.ratio, verdict: r.verdict, confidence: r.confidence }; }); items.push(...(await directCardsByUids(uids))); items.sort((a, b) => uids.indexOf(a.uid) - uids.indexOf(b.uid)); return NextResponse.json({ items }); } catch (e) { return NextResponse.json({ items: [], error: (e as Error).message }, { status: 503 }); } }