"use server";

import { prisma } from "@/lib/prisma";
import { revalidatePath } from "next/cache";

export async function createBlogPost(data: {
  title: string;
  excerpt: string;
  content: string;
  author: string;
  imageUrl: string;
}) {
  try {
    const post = await prisma.blogPost.create({
      data: {
        ...data,
      },
    });
    revalidatePath("/blog");
    return { success: true, data: post };
  } catch (error) {
    console.error("Failed to create blog post:", error);
    return { success: false, error: "Erreur lors de la création de l'article" };
  }
}

export async function updateBlogPost(id: string, data: {
  title: string;
  excerpt: string;
  content: string;
  author: string;
  imageUrl: string;
}) {
  try {
    const post = await prisma.blogPost.update({
      where: { id },
      data,
    });
    revalidatePath("/blog");
    return { success: true, data: post };
  } catch (error) {
    console.error("Failed to update blog post:", error);
    return { success: false, error: "Erreur lors de la mise à jour" };
  }
}

export async function deleteBlogPost(id: string) {
  try {
    await prisma.blogPost.delete({
      where: { id },
    });
    revalidatePath("/blog");
    return { success: true };
  } catch (error) {
    console.error("Failed to delete blog post:", error);
    return { success: false, error: "Erreur lors de la suppression" };
  }
}
