"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 requestToJoin(houseId: string) {
  try {
    const session = await auth();

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

    const request = await houseService.requestToJoin({
      houseId,
      requesterId: session.user.id,
    });

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

    return {
      success: true,
      message: "Demande envoyée",
      data: request,
    };
  } catch (error) {
    console.error("Request to join error:", error);

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

    return {
      success: false,
      error: "Une erreur est survenue lors de l'envoi de la demande",
    };
  }
}

export async function acceptRequest(requestId: string) {
  try {
    const session = await auth();

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

    const membership = await houseService.acceptRequest(
      requestId,
      session.user.id
    );

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

    return {
      success: true,
      message: "Demande acceptée",
      data: membership,
    };
  } catch (error) {
    console.error("Accept request error:", error);

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

    return {
      success: false,
      error: "Une erreur est survenue lors de l'acceptation de la demande",
    };
  }
}

export async function rejectRequest(requestId: string) {
  try {
    const session = await auth();

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

    await houseService.rejectRequest(requestId, session.user.id);

    revalidatePath("/houses");

    return { success: true, message: "Demande refusée" };
  } catch (error) {
    console.error("Reject request error:", error);

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

    return {
      success: false,
      error: "Une erreur est survenue lors du refus de la demande",
    };
  }
}

export async function cancelRequest(requestId: string) {
  try {
    const session = await auth();

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

    await houseService.cancelRequest(requestId, session.user.id);

    revalidatePath("/houses");

    return { success: true, message: "Demande annulée" };
  } catch (error) {
    console.error("Cancel request error:", error);

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

    return {
      success: false,
      error: "Une erreur est survenue lors de l'annulation de la demande",
    };
  }
}

