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

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 })
        }

        const unreadCount = await prisma.message.count({
            where: {
                conversation: {
                    participants: {
                        some: { userId: userId }
                    }
                },
                senderId: { not: userId },
                read: false
            }
        })

        return NextResponse.json({ count: unreadCount })
    } catch (error) {
        console.error('GET /api/messages/unread error:', error)
        return NextResponse.json({ error: 'Erreur serveur' }, { status: 500 })
    }
}
