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

export const dynamic = 'force-dynamic'

export async function GET() {
  try {
    const products = await prisma.smmProduct.findMany({
      where: { isActive: true },
      orderBy: [
        { category: 'asc' },
        { price: 'asc' }
      ]
    })

    // Group by category
    const categories: Record<string, any[]> = {}
    products.forEach(p => {
      if (!categories[p.category]) {
        categories[p.category] = []
      }
      categories[p.category].push(p)
    })

    return NextResponse.json(categories)
  } catch (error) {
    return NextResponse.json({ error: 'Gagal memuat produk SMM' }, { status: 500 })
  }
}
