import { NextRequest, NextResponse } from 'next/server' import { auth } from '@/auth' import { prisma } from '@/lib/prisma' export async function PUT( request: NextRequest, { params }: { params: Promise<{ id: string }> } ) { try { const session = await auth() if (!session?.user?.id) { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) } const { id: addressId } = await params // Check if address belongs to user const existingAddress = await prisma.address.findFirst({ where: { id: addressId, userId: session.user.id } }) if (!existingAddress) { return NextResponse.json({ error: 'Address not found' }, { status: 404 }) } // Unset all other default addresses for this user await prisma.address.updateMany({ where: { userId: session.user.id, isDefault: true }, data: { isDefault: false } }) // Set this address as default const address = await prisma.address.update({ where: { id: addressId }, data: { isDefault: true } }) return NextResponse.json({ address }) } catch (error) { console.error('Error setting default address:', error) return NextResponse.json( { error: 'Failed to set default address' }, { status: 500 } ) } }