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

// GET /api/admin/reports — List all reports for moderation
export async function GET(request: NextRequest) {
    try {
        const userId = request.headers.get('x-user-id')

        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 reports = await prisma.messageReport.findMany({
            include: {
                message: {
                    include: {
                        sender: {
                            select: { id: true, name: true, email: true }
                        },
                        conversation: {
                            include: {
                                business: {
                                    select: { id: true, name: true }
                                }
                            }
                        }
                    }
                },
                reporter: {
                    select: { id: true, name: true, email: true }
                }
            },
            orderBy: {
                createdAt: 'desc'
            }
        })

        return NextResponse.json(reports)
    } catch (error) {
        console.error('GET /api/admin/reports error:', error)
        return NextResponse.json({ error: 'Erreur serveur' }, { status: 500 })
    }
}
