import { auth } from "@/auth";
import { redirect } from "next/navigation";
import { getFileNodes } from "./actions";
import DashboardView from "./dashboard-view";
import { Box, Typography, Chip } from "@mui/material";
import AdminPanelSettingsIcon from '@mui/icons-material/AdminPanelSettings';

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

  // Guard: If not logged in, go back to home
  if (!session?.user) {
    redirect("/");
  }

  // Fetch initial files from PostgreSQL
  const initialFiles = await getFileNodes();

  // Determine admin status for the header display
  // @ts-ignore
  const isAdmin = session.user.role === "ADMIN";

  return (
    <main className="p-8">
      <Box sx={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', mb: 6 }}>
        <Box>
          <Typography variant="h4" fontWeight={800} sx={{ color: 'text.primary' }}>
            My OneDrive Library
          </Typography>
          <Typography variant="body1" color="text.secondary">
            Manage your synchronized files and project folders.
          </Typography>
        </Box>

        {isAdmin && (
          <Chip 
            icon={<AdminPanelSettingsIcon />} 
            label="Admin Access" 
            color="primary" 
            variant="outlined" 
            sx={{ fontWeight: 600 }}
          />
        )}
      </Box>
      
      {/* Pass both initialFiles AND the user object.
        The DashboardView will use user.role and user.id to 
        decide who can see the 'Delete' button.
      */}
      <DashboardView 
        initialFiles={initialFiles} 
        user={session.user} 
      />
    </main>
  );
}