import prisma from '~/lib/prisma';

export default defineEventHandler(async (event) => {
  try {
    const body = await readBody(event);
    const { content, articleId, likes, dislikes } = body;

    if (!content || !articleId) {
      return {
        success: false,
        message: 'Content and articleId are required to create a comment.',
      };
    }

    const newComment = await prisma.comment.create({
      data: {
        content,
        articleId,
        likes: likes || 0,
        dislikes: dislikes || 0,
      },
    });

    return {
      success: true,
      message: 'Comment created successfully',
      data: newComment,
    };
  } catch (error) {
    console.error('Error creating comment:', error);
    return {
      success: false,
      message: 'An error occurred while creating the comment',
      error: error.message,
    };
  }
});
