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

// PATCH /api/conversations/[id]/archive — Toggle archive status for the user
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 { archived } = await request.json()

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

        const updatedParticipant = await prisma.conversationParticipant.update({
            where: {
                userId_conversationId: {
                    userId: userId,
                    conversationId: id
                }
            },
            data: {
                isArchived: archived ?? true
            }
        })

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