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

// GET /api/businesses/me — Fetch business for the current user
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 business = await prisma.business.findFirst({
            where: { ownerId: userId },
            include: { offers: true }
        })

        if (!business) {
            return NextResponse.json(null) // Return null if no business yet
        }

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