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

// GET /api/users
export async function GET() {
    try {
        const users = await prisma.user.findMany({
            include: { businesses: true },
            orderBy: { createdAt: 'desc' },
        })
        return NextResponse.json(users)
    } catch (error) {
        console.error('GET /api/users error:', error)
        return NextResponse.json({ error: 'Erreur serveur' }, { status: 500 })
    }
}

// POST /api/users
export async function POST(request: NextRequest) {
    try {
        const body = await request.json()
        const user = await prisma.user.create({ data: body })
        return NextResponse.json(user, { status: 201 })
    } catch (error) {
        console.error('POST /api/users error:', error)
        return NextResponse.json({ error: 'Erreur lors de la création' }, { status: 500 })
    }
}
