"use server";

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

export async function toggleVerification(id: string, currentStatus: boolean) {
  try {
    await prisma.business.update({
      where: { id },
      data: { verified: !currentStatus },
    });
    revalidatePath("/entrepreneurs");
    revalidatePath("/dashboard");
    return { success: true };
  } catch (error) {
    console.error("Failed to toggle verification:", error);
    return { success: false, error: "Erreur lors de la mise à jour" };
  }
}

export async function setFeaturedBusiness(id: string, data: {
  founderName: string;
  founderImageUrl: string;
  keyMetric: string;
}) {
  try {
    // 1. Reset all others (we only want one entrepreneur of the month)
    await prisma.business.updateMany({
      where: { isFeatured: true },
      data: { isFeatured: false },
    });

    // 2. Set the new one
    await prisma.business.update({
      where: { id },
      data: {
        isFeatured: true,
        ...data,
      },
    });

    revalidatePath("/entrepreneurs");
    revalidatePath("/dashboard");
    return { success: true };
  } catch (error) {
    console.error("Failed to set featured business:", error);
    return { success: false, error: "Erreur lors de la mise à jour de l'entrepreneur du mois" };
  }
}

export async function removeFeaturedBusiness(id: string) {
  try {
    await prisma.business.update({
      where: { id },
      data: { isFeatured: false },
    });
    revalidatePath("/entrepreneurs");
    return { success: true };
  } catch (error) {
    console.error("Failed to remove featured status:", error);
    return { success: false, error: "Erreur lors de la suppression" };
  }
}
