import prisma from '~/lib/prisma';

export default defineEventHandler(async (event) => {
  const id = event.context.params?.id;
  const body = await readBody(event);

  if (!id) {
    return {
      success: false,
      message: 'Invalid article ID.',
    };
  }

  try {
    const updatedArticle = await prisma.article.update({
      where: { id },
      data: {
        title: body.title,
        description: body.description,
        likes: body.likes,
        dislikes: body.dislikes,
      },
    });

    return {
      success: true,
      data: updatedArticle,
    };
  } catch (error) {
    console.error('Error updating article:', error);
    return {
      success: false,
      error: error.message,
    };
  }
});
