import { NextResponse } from 'next/server'
import { db } from '@/lib/db'

export async function GET() {
  try {
    const categories = await db.category.findMany({
      orderBy: { order: 'asc' },
      include: {
        _count: { select: { products: true } },
      },
    })

    return NextResponse.json({
      status: 'success',
      data: categories.map(c => ({
        ...c,
        productCount: c._count.products,
        _count: undefined,
      })),
    })
  } catch (error) {
    console.error('Error fetching categories:', error)
    return NextResponse.json(
      { status: 'error', message: 'Erro ao buscar categorias' },
      { status: 500 }
    )
  }
}
