import { requireSuperAdmin } from '../../utils/auth'
import { useDB, eq } from '../../utils/database'
import { products } from '../../database/schema'
import { handleApiError, createSuccessResponse } from '../../utils/errors'

interface ProductResponse {
  id: string
  name: string
}

export default defineEventHandler(async (event) => {
  try {
    await requireSuperAdmin(event)

    const db = useDB()
    const activeProducts = await db
      .select({
        id: products.id,
        name: products.name
      })
      .from(products)
      .where(eq(products.active, true))
      .orderBy(products.name)

    return createSuccessResponse<ProductResponse[]>(activeProducts, 'Products retrieved successfully')
  } catch (error) {
    throw handleApiError(error)
  }
})
