"use server";

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

export async function createInterview(data: {
  title: string;
  guestName: string;
  companyName: string;
  role: string;
  type: InterviewType;
  thumbnailUrl: string;
  videoUrl?: string;
  content?: string;
  excerpt: string;
  duration?: string;
}) {
  try {
    const interview = await prisma.interview.create({
      data,
    });
    revalidatePath("/blog");
    return { success: true, data: interview };
  } catch (error) {
    console.error("Failed to create interview:", error);
    return { success: false, error: "Erreur lors de la création de l'interview" };
  }
}

export async function updateInterview(id: string, data: {
  title: string;
  guestName: string;
  companyName: string;
  role: string;
  type: InterviewType;
  thumbnailUrl: string;
  videoUrl?: string;
  content?: string;
  excerpt: string;
  duration?: string;
}) {
  try {
    const interview = await prisma.interview.update({
      where: { id },
      data,
    });
    revalidatePath("/blog");
    return { success: true, data: interview };
  } catch (error) {
    console.error("Failed to update interview:", error);
    return { success: false, error: "Erreur lors de la mise à jour" };
  }
}

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