import { NextRequest, NextResponse } from 'next/server'
import { authenticateH2H, h2hUnauthorized } from '@/lib/h2h-auth'
import prisma from '@/lib/prisma'

export const dynamic = 'force-dynamic'

export async function GET(req: NextRequest) {
  const user = await authenticateH2H(req)
  if (!user) return h2hUnauthorized()

  try {
    const products = await prisma.digitalProduct.findMany({
      where: { isActive: true },
      select: { id: true, name: true, slug: true, description: true, price: true, stock: true, thumbnail: true, type: true },
      orderBy: { sortOrder: 'asc' },
    })

    return NextResponse.json({ success: true, data: products })
  } catch (error) {
    return NextResponse.json({ success: false, error: 'Gagal memuat produk' }, { status: 500 })
  }
}
