import Link from "next/link"
import { EmptyState } from "@/shared/components/ui/empty-state"
import { Badge } from "@/shared/components/ui/badge"
import {
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow,
} from "@/shared/components/ui/table"
import { formatDate } from "@/shared/lib/utils"
import { getHomeworkAssignmentReviewList } from "@/modules/homework/data-access"
import { Inbox } from "lucide-react"
import { getTeacherIdForMutations } from "@/modules/classes/data-access"

export const dynamic = "force-dynamic"

export default async function SubmissionsPage() {
  const creatorId = await getTeacherIdForMutations()
  const assignments = await getHomeworkAssignmentReviewList({ creatorId })
  const hasAssignments = assignments.length > 0

  return (
    <div className="h-full flex-1 flex-col space-y-8 p-8 md:flex">
      <div className="flex items-center justify-between space-y-2">
        <div>
          <h2 className="text-2xl font-bold tracking-tight">Submissions</h2>
          <p className="text-muted-foreground">
            Review homework by assignment.
          </p>
        </div>
      </div>

      {!hasAssignments ? (
        <EmptyState
          title="No assignments"
          description="There are no homework assignments to review yet."
          icon={Inbox}
        />
      ) : (
        <div className="rounded-md border bg-card">
          <Table>
            <TableHeader>
              <TableRow>
                <TableHead>Assignment</TableHead>
                <TableHead>Status</TableHead>
                <TableHead>Due</TableHead>
                <TableHead className="text-right">Targets</TableHead>
                <TableHead className="text-right">Submitted</TableHead>
                <TableHead className="text-right">Graded</TableHead>
              </TableRow>
            </TableHeader>
            <TableBody>
              {assignments.map((a) => (
                <TableRow key={a.id}>
                  <TableCell className="font-medium">
                    <Link href={`/teacher/homework/assignments/${a.id}/submissions`} className="hover:underline">
                      {a.title}
                    </Link>
                    <div className="text-xs text-muted-foreground">{a.sourceExamTitle}</div>
                  </TableCell>
                  <TableCell>
                    <Badge variant="outline" className="capitalize">
                      {a.status}
                    </Badge>
                  </TableCell>
                  <TableCell className="text-muted-foreground">{a.dueAt ? formatDate(a.dueAt) : "-"}</TableCell>
                  <TableCell className="text-right">{a.targetCount}</TableCell>
                  <TableCell className="text-right">{a.submittedCount}</TableCell>
                  <TableCell className="text-right">{a.gradedCount}</TableCell>
                </TableRow>
              ))}
            </TableBody>
          </Table>
        </div>
      )}
    </div>
  )
}
