import { requireUserSession } from '../../utils/auth'
import { useDB, eq, desc } from '../../utils/database'
import { feedback as feedbackTable } from '../../database/schema'
import { handleApiError, createSuccessResponse } from '../../utils/errors'

export default defineEventHandler(async (event) => {
  try {
    const session = await requireUserSession(event)

    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    const db = useDB() as any
    const rows = await db.query.feedback.findMany({
      where: eq(feedbackTable.userId, session.user.id),
      orderBy: [desc(feedbackTable.createdAt)]
    })

    return createSuccessResponse(rows, 'Feedback retrieved successfully')
  } catch (error) {
    throw handleApiError(error)
  }
})
