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

// PATCH /api/admin/reports/[id] — Update report status
export async function PATCH(
    request: NextRequest,
    { params }: { params: Promise<{ id: string }> }
) {
    try {
        const userId = request.headers.get('x-user-id')
        const { id } = await params
        const { status } = await request.json()

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

        // Check if user is admin
        const user = await prisma.user.findUnique({
            where: { id: userId },
            select: { role: true }
        })

        if (!user || user.role !== 'ADMIN') {
            return NextResponse.json({ error: 'Accès réservé aux administrateurs' }, { status: 403 })
        }

        const report = await prisma.messageReport.update({
            where: { id },
            data: { status }
        })

        return NextResponse.json(report)
    } catch (error) {
        console.error('PATCH /api/admin/reports/[id] error:', error)
        return NextResponse.json({ error: 'Erreur serveur' }, { status: 500 })
    }
}
