import { auth } from "@/auth";
import { redirect } from "next/navigation";
import UploadView from "./upload-view"; // This is the Client Component
import { Container } from "@mui/material";
import { prisma } from "@/lib/prisma";

// 1. Rename to UploadPage to avoid conflict with the 'UploadView' import
// 2. Add 'async' so you can use 'await' inside
export default async function UploadPage() {
  const session = await auth();
  
  if (!session) redirect("/");

  // 3. Fetch folders. Renamed variable to 'allFolders' to avoid any confusion
  const allFolders = await prisma.fileNode.findMany({
    where: { 
      isFolder: true,
      ownerId: session.user.id // Good practice: only show user's own folders
    },
    orderBy: { name: 'asc' },
    select: { id: true, name: true, parentId: true }
  });

  return (
    <Container maxWidth="md" sx={{ py: 8 }}>
      {/* 4. Render the Client Component and pass the data */}
      <UploadView user={session.user} folders={allFolders} />
    </Container>
  );
}