import { NextRequest, NextResponse } from "next/server"; import { prisma } from "@/lib/db"; export async function GET(req: NextRequest) { try { const { searchParams } = new URL(req.url); const mode = searchParams.get("mode") || ""; const where = mode ? { searchMode: mode } : {}; const history = await prisma.searchHistory.findMany({ where, orderBy: { executedAt: "desc" }, take: 50, select: { query: true, region: true, searchMode: true, executedAt: true }, }); return NextResponse.json(history); } catch (err) { console.error("GET /api/search-history error:", err); return NextResponse.json({ error: "Failed to fetch search history" }, { status: 500 }); } } export async function POST(req: NextRequest) { try { const body = await req.json() as { query: string; region: string; searchMode: string }; const { query, region, searchMode } = body; if (!query?.trim() || !searchMode?.trim()) { return NextResponse.json({ error: "query und searchMode sind erforderlich" }, { status: 400 }); } const entry = await prisma.searchHistory.create({ data: { query: query.trim(), region: (region || "").trim(), searchMode: searchMode.trim(), }, }); return NextResponse.json(entry); } catch (err) { console.error("POST /api/search-history error:", err); return NextResponse.json({ error: "Failed to save search history" }, { status: 500 }); } }