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;
    const house = await houseService.getHouseById(houseId, {
      memberships: {
        include: {
          user: {
            select: {
              id: true,
              username: true,
              avatar: true,
              score: true,
              level: true,
            },
          },
        },
      },
      creator: {
        select: {
          id: true,
          username: true,
          avatar: true,
        },
      },
    });

    if (!house) {
      return NextResponse.json(
        { error: "Maison non trouvée" },
        { status: 404 }
      );
    }

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

