import { redirect } from "next/navigation";
import { auth } from "@/lib/auth";
import { db } from "@tasks/database/client";
import { objects, workspaceMembers } from "@tasks/database/schema";
import { eq, and } from "drizzle-orm";

export default async function HomePage() {
  const session = await auth();

  if (!session?.user?.id) {
    redirect("/sign-in");
  }

  const membership = await db
    .select({ workspaceId: workspaceMembers.workspaceId })
    .from(workspaceMembers)
    .where(eq(workspaceMembers.userId, session.user.id))
    .limit(1);

  if (membership.length > 0) {
    redirect(`/${membership[0].workspaceId}`);
  }

  redirect("/sign-in");
}
