"use server";

import { appRoutes } from "@/config";
import {
  articleSchema,
  createArticleSchema,
} from "@/lib/validation/zod/article";
import { api } from "@/trpc/server";
import { revalidatePath } from "next/cache";
import { redirect } from "next/navigation";
import { z } from "zod";

export async function createArticle(
  article: z.infer<typeof createArticleSchema>,
) {
  const result = await api.article.create({
    article,
  });
  if (!result[0]?.slug?.length) return false;
  revalidatePath("/");
  return redirect(appRoutes.editArticle(result[0].slug));
}
export async function updateArticle(
  article: z.infer<typeof articleSchema>,
  articleId: string,
) {
  const result = await api.article.update({
    article: { ...article, content: JSON.parse(article.content) },
    articleId,
  });
  revalidatePath("/");

  //   if (!result[0]?.id?.length) return false;
  // return revalidatePath(`/artikel/${result[0]?.slug}/edit`);
}
export async function deleteArticle(articleId: string) {
  const result = await api.article.delete({
    articleId,
  });
  revalidatePath("/");

  // if (!result[0]?.id?.length) return false;
}
