import prisma from '~/lib/prisma';

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

    const newArticle = await prisma.article.create({
      data: {
        title,
        description,
        likes: likes || 0,
        dislikes: dislikes || 0,
      },
    });

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