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

export default defineEventHandler(async (event) => {
  try {
    // Fetch active user segments
    const segments = await prisma.user_segments.findMany({
      where: {
        is_active: true,
      },
      select: {
        id: true,
        name: true,
        value: true,
        description: true,
      },
      orderBy: {
        name: "asc",
      },
    });

    return segments.map((segment) => ({
      id: segment.id,
      name: segment.name,
      value: segment.value,
      description: segment.description,
    }));
  } catch (error) {
    console.error("Error fetching segments:", error);
    throw createError({
      statusCode: 500,
      statusMessage: "Failed to fetch user segments",
      data: {
        error: error.message,
      },
    });
  } finally {
  }
}); 