"use server";

import { revalidatePath } from "next/cache";
import { auth } from "@/lib/auth";
import { houseService } from "@/services/houses/house.service";
import {
  ValidationError,
  ConflictError,
  ForbiddenError,
  NotFoundError,
} from "@/services/errors";

export async function updateHouse(
  houseId: string,
  data: {
    name?: string;
    description?: string | null;
  }
) {
  try {
    const session = await auth();

    if (!session?.user?.id) {
      return {
        success: false,
        error: "Vous devez être connecté",
      };
    }

    const house = await houseService.updateHouse(houseId, session.user.id, data);

    revalidatePath("/houses");
    revalidatePath(`/houses/${houseId}`);

    return { success: true, message: "Maison mise à jour", data: house };
  } catch (error) {
    console.error("Update house error:", error);

    if (
      error instanceof ValidationError ||
      error instanceof ConflictError ||
      error instanceof ForbiddenError
    ) {
      return { success: false, error: error.message };
    }

    return {
      success: false,
      error: "Une erreur est survenue lors de la mise à jour de la maison",
    };
  }
}

export async function deleteHouse(houseId: string) {
  try {
    const session = await auth();

    if (!session?.user?.id) {
      return {
        success: false,
        error: "Vous devez être connecté",
      };
    }

    await houseService.deleteHouse(houseId, session.user.id);

    revalidatePath("/houses");
    revalidatePath("/profile");

    return { success: true, message: "Maison supprimée" };
  } catch (error) {
    console.error("Delete house error:", error);

    if (error instanceof ForbiddenError) {
      return { success: false, error: error.message };
    }

    return {
      success: false,
      error: "Une erreur est survenue lors de la suppression de la maison",
    };
  }
}

export async function leaveHouse(houseId: string) {
  try {
    const session = await auth();

    if (!session?.user?.id) {
      return {
        success: false,
        error: "Vous devez être connecté",
      };
    }

    await houseService.leaveHouse(houseId, session.user.id);

    revalidatePath("/houses");
    revalidatePath("/profile");

    return { success: true, message: "Vous avez quitté la maison" };
  } catch (error) {
    console.error("Leave house error:", error);

    if (error instanceof ForbiddenError) {
      return { success: false, error: error.message };
    }

    return {
      success: false,
      error: "Une erreur est survenue lors de la sortie de la maison",
    };
  }
}

export async function removeMember(houseId: string, memberId: string) {
  try {
    const session = await auth();

    if (!session?.user?.id) {
      return {
        success: false,
        error: "Vous devez être connecté",
      };
    }

    await houseService.removeMember(houseId, memberId, session.user.id);

    revalidatePath("/houses");
    revalidatePath("/profile");

    return { success: true, message: "Membre retiré de la maison" };
  } catch (error) {
    console.error("Remove member error:", error);

    if (
      error instanceof ForbiddenError ||
      error instanceof NotFoundError
    ) {
      return { success: false, error: error.message };
    }

    return {
      success: false,
      error: "Une erreur est survenue lors du retrait du membre",
    };
  }
}

