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

type Params = { params: Promise<{ id: string }> }

// GET /api/offers/[id]
export async function GET(_request: NextRequest, { params }: Params) {
    try {
        const { id } = await params
        const offer = await prisma.offer.findUnique({
            where: { id },
            include: { business: true },
        })
        if (!offer) return NextResponse.json({ error: 'Non trouvé' }, { status: 404 })
        return NextResponse.json(offer)
    } catch (error) {
        console.error('GET /api/offers/[id] error:', error)
        return NextResponse.json({ error: 'Erreur serveur' }, { status: 500 })
    }
}

// PATCH /api/offers/[id]
export async function PATCH(request: NextRequest, { params }: Params) {
    try {
        const { id } = await params
        const body = await request.json()
        const offer = await prisma.offer.update({
            where: { id },
            data: body,
            include: { business: true },
        })
        return NextResponse.json(offer)
    } catch (error) {
        console.error('PATCH /api/offers/[id] error:', error)
        return NextResponse.json({ error: 'Erreur lors de la mise à jour' }, { status: 500 })
    }
}

// DELETE /api/offers/[id]
export async function DELETE(_request: NextRequest, { params }: Params) {
    try {
        const { id } = await params
        await prisma.offer.delete({ where: { id } })
        return NextResponse.json({ success: true })
    } catch (error) {
        console.error('DELETE /api/offers/[id] error:', error)
        return NextResponse.json({ error: 'Erreur lors de la suppression' }, { status: 500 })
    }
}
