"use server";

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

/**
 * Suspend a user account
 */
export async function suspendUser(userId: string, reason: string) {
  try {
    const user = await prisma.user.update({
      where: { id: userId },
      data: {
        isSuspended: true,
        suspensionReason: reason,
      },
      include: {
          businesses: true
      }
    });

    // If the user is an entrepreneur with a business, we also suspend the business by default
    // to hide it from the site during the user's suspension.
    if (user.businesses) {
      await prisma.business.update({
        where: { id: user.businesses.id },
        data: {
          isSuspended: true,
          suspensionReason: `Compte propriétaire suspendu : ${reason}`,
        },
      });
    }

    revalidatePath("/clients");
    revalidatePath("/entrepreneurs");
    revalidatePath("/moderation");
    
    return { success: true, data: user };
  } catch (error) {
    console.error("Failed to suspend user:", error);
    return { success: false, error: "Erreur lors de la suspension" };
  }
}

/**
 * Unsuspend a user account
 */
export async function unsuspendUser(userId: string) {
  try {
    const user = await prisma.user.update({
      where: { id: userId },
      data: {
        isSuspended: false,
        suspensionReason: null,
      },
      include: {
          businesses: true
      }
    });

    // Also unsuspend their business if they had one
    if (user.businesses) {
      await prisma.business.update({
        where: { id: user.businesses.id },
        data: {
          isSuspended: false,
          suspensionReason: null,
        },
      });
    }

    revalidatePath("/clients");
    revalidatePath("/entrepreneurs");
    revalidatePath("/moderation");
    
    return { success: true, data: user };
  } catch (error) {
    console.error("Failed to unsuspend user:", error);
    return { success: false, error: "Erreur lors de la réactivation" };
  }
}

/**
 * Suspend a specific business (without suspending the whole user account)
 */
export async function suspendBusiness(businessId: string, reason: string) {
  try {
    const business = await prisma.business.update({
      where: { id: businessId },
      data: {
        isSuspended: true,
        suspensionReason: reason,
      },
    });

    revalidatePath("/entrepreneurs");
    
    return { success: true, data: business };
  } catch (error) {
    console.error("Failed to suspend business:", error);
    return { success: false, error: "Erreur lors de la suspension de la boutique" };
  }
}

/**
 * Unsuspend a specific business
 */
export async function unsuspendBusiness(businessId: string) {
  try {
    const business = await prisma.business.update({
      where: { id: businessId },
      data: {
        isSuspended: false,
        suspensionReason: null,
      },
    });

    revalidatePath("/entrepreneurs");
    
    return { success: true, data: business };
  } catch (error) {
    console.error("Failed to unsuspend business:", error);
    return { success: false, error: "Erreur lors de la réactivation de la boutique" };
  }
}
