import { NextResponse } from "next/server";
import { auth } from "@/lib/auth";
import { houseService } from "@/services/houses/house.service";

export async function GET(
  request: Request,
  { params }: { params: Promise<{ houseId: string }> }
) {
  try {
    const session = await auth();

    if (!session?.user?.id) {
      return NextResponse.json(
        { error: "Vous devez être connecté" },
        { status: 401 }
      );
    }

    const { houseId } = await params;

    // Vérifier que l'utilisateur est membre de la maison
    const isMember = await houseService.isUserMemberOfHouse(
      session.user.id,
      houseId
    );

    if (!isMember) {
      return NextResponse.json(
        { error: "Vous devez être membre de cette maison" },
        { status: 403 }
      );
    }

    const { searchParams } = new URL(request.url);
    const status = searchParams.get("status") as "PENDING" | "ACCEPTED" | "REJECTED" | "CANCELLED" | null;

    const invitations = await houseService.getHouseInvitations(
      houseId,
      status || undefined
    );

    return NextResponse.json(invitations);
  } catch (error) {
    console.error("Error fetching house invitations:", error);
    return NextResponse.json(
      { error: "Erreur lors de la récupération des invitations" },
      { status: 500 }
    );
  }
}

