import { NextRequest, NextResponse } from 'next/server'
import prisma from '../../../../lib/prisma'

// PATCH /api/users/me — Update personal user profile
export async function PATCH(request: NextRequest) {
    try {
        const userId = request.headers.get('x-user-id')
        const data = await request.json()

        if (!userId) {
            return NextResponse.json({ error: 'Utilisateur non identifié' }, { status: 401 })
        }

        const updatedUser = await prisma.user.update({
            where: { id: userId },
            data: {
                name: data.name,
                phone: data.phone,
                bio: data.bio,
                location: data.location,
            },
            select: {
                id: true,
                name: true,
                email: true,
                role: true,
                phone: true,
                bio: true,
                location: true,
                avatar: true
            }
        })

        return NextResponse.json(updatedUser)
    } catch (error) {
        console.error('PATCH /api/users/me error:', error)
        return NextResponse.json({ error: 'Erreur lors de la mise à jour' }, { status: 500 })
    }
}
