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

export const dynamic = 'force-dynamic'

export async function GET(req: NextRequest) {
  try {
    const { searchParams } = new URL(req.url)
    const search = searchParams.get('search') || ''

    const categories = await prisma.gameCategory.findMany({
      where: {
        isActive: true,
        ...(search && {
          name: { contains: search },
        }),
      },
      include: {
        _count: {
          select: {
            products: { where: { isActive: true, status: true } },
          },
        },
      },
      orderBy: { sortOrder: 'asc' },
    })

    return NextResponse.json({ categories })
  } catch (error) {
    console.error('[GAMES GET]', error)
    return NextResponse.json({ error: 'Gagal memuat kategori game' }, { status: 500 })
  }
}
