import { prisma } from "@/lib/prisma";
import { getServerSession } from "next-auth/next";
import { authOptions } from "@/lib/auth-config";
import { redirect } from "next/navigation";
import { Table, TableHeader, TableBody, TableRow, TableHead, TableCell } from "@/components/ui/table";
import { Card, CardContent } from "@/components/ui/card";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import Link from "next/link";

export const dynamic = "force-dynamic";

export default async function StudentsPage() {
  const session = await getServerSession(authOptions);
  if (!session?.user) redirect("/login");
  if (!session.user.establishmentId && session.user.role !== "superadmin") redirect("/dashboard");

  const establishmentId = session.user.establishmentId;
  const students = await prisma.student.findMany({
    where: establishmentId ? { class: { establishmentId } } : {},
    include: { class: true, nodes: true },
    orderBy: { createdAt: "desc" },
  });

  return (
    <div className="space-y-6">
      <div className="flex items-center justify-between">
        <h1 className="text-3xl font-bold">Étudiants</h1>
        <Link href="/dashboard/students/new">
          <Button>Ajouter</Button>
        </Link>
      </div>
      <Card>
        <CardContent className="pt-6">
          <Table>
            <TableHeader>
              <TableRow>
                <TableHead>Nom</TableHead>
                <TableHead>Classe</TableHead>
                <TableHead>Email</TableHead>
                <TableHead>Code activation</TableHead>
                <TableHead>Nœud</TableHead>
              </TableRow>
            </TableHeader>
            <TableBody>
              {students.map((s) => {
                const node = s.nodes[0];
                return (
                  <TableRow key={s.id}>
                    <TableCell className="font-medium">
                      <Link href={`/dashboard/students/${s.id}`} className="hover:underline">
                        {s.firstName} {s.lastName}
                      </Link>
                    </TableCell>
                    <TableCell>{s.class.name}</TableCell>
                    <TableCell>{s.email}</TableCell>
                    <TableCell>{s.activationCode || "-"}</TableCell>
                    <TableCell>
                      {node ? (
                        <Badge variant={node.status === "online" ? "success" : "secondary"}>{node.status}</Badge>
                      ) : (
                        <Badge variant="outline">Non lié</Badge>
                      )}
                    </TableCell>
                  </TableRow>
                );
              })}
              {students.length === 0 && (
                <TableRow>
                  <TableCell colSpan={5} className="text-center text-muted-foreground">Aucun étudiant</TableCell>
                </TableRow>
              )}
            </TableBody>
          </Table>
        </CardContent>
      </Card>
    </div>
  );
}
