import prisma from "~/server/utils/prisma";

export default defineEventHandler(async (event) => {
  try {
    // Fetch notification categories from the database
    const categories = await prisma.notification_categories.findMany({
      select: {
        name: true,
        value: true,
        description: true,
      },
      orderBy: {
        name: 'asc'
      }
    });

    // Transform the data to match the expected format
    const formattedCategories = categories.map(category => ({
      label: category.name,
      value: category.value,
      description: category.description
    }));

    return {
      success: true,
      data: formattedCategories
    }
  } catch (error) {
    console.error('Error fetching categories:', error)
    throw createError({
      statusCode: 500,
      statusMessage: 'Failed to fetch categories'
    })
  } finally {
    // Disconnect Prisma client to avoid connection leaks
  }
}) 