import { NextRequest, NextResponse } from "next/server" import { prisma } from "@/lib/prisma" // POST - Capture a prompt from AI chat export async function POST(request: NextRequest) { try { const body = await request.json() const { userId, deviceId, title, text, platform, url, tags } = body if (!deviceId || !text || !platform) { return NextResponse.json( { error: "deviceId, text, and platform are required" }, { status: 400 } ) } const captured = await prisma.capturedPrompt.create({ data: { userId: userId || null, deviceId, title: title || null, text, platform, url: url || null, tags: tags || [], }, }) // Also record this as an analytics event await prisma.extensionAnalytics.create({ data: { userId: userId || null, deviceId, action: "capture", platform, promptText: text.slice(0, 500), // Store first 500 chars source: "extension", }, }) return NextResponse.json({ success: true, id: captured.id, captured }) } catch (error) { console.error("Capture prompt error:", error) return NextResponse.json( { error: "Failed to capture prompt" }, { status: 500 } ) } } // GET - Fetch captured prompts for a user/device export async function GET(request: NextRequest) { try { const { searchParams } = new URL(request.url) const userId = searchParams.get("userId") const deviceId = searchParams.get("deviceId") const limit = parseInt(searchParams.get("limit") || "50") if (!userId && !deviceId) { return NextResponse.json( { error: "userId or deviceId required" }, { status: 400 } ) } const where: { userId?: string; deviceId?: string } = {} if (userId) where.userId = userId if (deviceId) where.deviceId = deviceId const captured = await prisma.capturedPrompt.findMany({ where, orderBy: { createdAt: "desc" }, take: limit, }) return NextResponse.json({ captured }) } catch (error) { console.error("Fetch captured prompts error:", error) return NextResponse.json( { error: "Failed to fetch captured prompts" }, { status: 500 } ) } } // DELETE - Delete a captured prompt export async function DELETE(request: NextRequest) { try { const { searchParams } = new URL(request.url) const id = searchParams.get("id") const deviceId = searchParams.get("deviceId") if (!id || !deviceId) { return NextResponse.json( { error: "id and deviceId required" }, { status: 400 } ) } // Verify ownership const existing = await prisma.capturedPrompt.findFirst({ where: { id, deviceId }, }) if (!existing) { return NextResponse.json( { error: "Captured prompt not found" }, { status: 404 } ) } await prisma.capturedPrompt.delete({ where: { id }, }) return NextResponse.json({ success: true }) } catch (error) { console.error("Delete captured prompt error:", error) return NextResponse.json( { error: "Failed to delete captured prompt" }, { status: 500 } ) } }