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 const data = await request.json() const { firstName, lastName, company, address1, address2, city, state, zipCode, country, phone, isDefault, type } = data // 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 }) } // If setting as default, unset other default addresses if (isDefault) { await prisma.address.updateMany({ where: { userId: session.user.id, isDefault: true, id: { not: addressId } }, data: { isDefault: false } }) } const address = await prisma.address.update({ where: { id: addressId }, data: { firstName, lastName, company, address1, address2, city, state, zipCode, country: country || 'India', phone, isDefault, type } }) return NextResponse.json({ address }) } catch (error) { console.error('Error updating address:', error) return NextResponse.json( { error: 'Failed to update address' }, { status: 500 } ) } } export async function DELETE( 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 }) } await prisma.address.delete({ where: { id: addressId } }) return NextResponse.json({ message: 'Address deleted successfully' }) } catch (error) { console.error('Error deleting address:', error) return NextResponse.json( { error: 'Failed to delete address' }, { status: 500 } ) } }