import { Article } from "../../models/Article";
import { requireRole } from "../../utils/auth";

export default defineEventHandler(async (event) => {
  const slug = getRouterParam(event, "slug");

  // Only admins can delete articles
  const user = await requireRole(event, ["admin"]);

  if (!slug) {
    throw createError({
      statusCode: 400,
      statusMessage: "Slug is required",
    });
  }

  // Find and delete article
  const article = await Article.findOneAndDelete({ slug });

  if (!article) {
    throw createError({
      statusCode: 404,
      statusMessage: "Article not found",
    });
  }

  return {
    success: true,
    message: "Article deleted successfully",
  };
});
