import { NextRequest, NextResponse } from "next/server"; import { prisma } from "@/lib/prisma"; import { requireAuth } from "@/lib/auth"; export async function POST(request: NextRequest) { try { const auth = await requireAuth(request); if (!auth) { return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); } const user = await prisma.user.findUnique({ where: { id: auth.userId } }); if (!user) { return NextResponse.json({ error: "User not found" }, { status: 404 }); } if (!user.isPro) { return NextResponse.json( { error: "Only Pro members can feature listings. Upgrade to Pro to access this feature." }, { status: 403 } ); } const { listingId } = await request.json(); if (!listingId) { return NextResponse.json( { error: "Missing required field: listingId" }, { status: 400 } ); } // Verify listing exists and belongs to user const listing = await prisma.listing.findUnique({ where: { id: listingId }, }); if (!listing) { return NextResponse.json({ error: "Listing not found" }, { status: 404 }); } if (listing.sellerId !== user.id) { return NextResponse.json( { error: "You can only feature your own listings" }, { status: 403 } ); } if (listing.status !== "active") { return NextResponse.json( { error: "Cannot feature a sold or inactive listing" }, { status: 400 } ); } if (listing.featured) { return NextResponse.json( { error: "Listing is already featured" }, { status: 400 } ); } // Check FLH balance const FEATURE_FEE = 100; if (user.flhBalance < FEATURE_FEE) { return NextResponse.json( { error: `Insufficient FLH balance. Featuring costs ${FEATURE_FEE} FLH. Your balance: ${user.flhBalance} FLH.` }, { status: 400 } ); } // Deduct fee and set featured const [updatedListing] = await prisma.$transaction([ prisma.listing.update({ where: { id: listingId }, data: { featured: true, featuredUntil: new Date(Date.now() + 30 * 24 * 60 * 60 * 1000), // 30 days }, }), prisma.user.update({ where: { id: user.id }, data: { flhBalance: { decrement: FEATURE_FEE } }, }), ]); return NextResponse.json({ listing: updatedListing, message: `Listing featured for 30 days. ${FEATURE_FEE} FLH deducted.`, }); } catch (error) { console.error("POST /api/marketplace/feature error:", error); return NextResponse.json( { error: "Internal server error" }, { status: 500 } ); } }