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

export async function GET() {
  try {
    const session = await auth();

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

    const house = await houseService.getUserHouse(session.user.id, {
      memberships: {
        include: {
          user: {
            select: {
              id: true,
              username: true,
              avatar: true,
              score: true,
              level: true,
            },
          },
        },
      },
      creator: {
        select: {
          id: true,
          username: true,
          avatar: true,
        },
      },
    });

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

