import Link from "next/link"
import { BookOpen, PenTool, Library, ArrowRight } from "lucide-react"

import { getStudentClasses } from "@/modules/classes/data-access"
import { getStudentHomeworkAssignments } from "@/modules/homework/data-access"
import { getCurrentStudentUser } from "@/modules/users/data-access"
import { getTextbooks } from "@/modules/textbooks/data-access"
import { Card, CardContent, CardHeader, CardTitle } from "@/shared/components/ui/card"
import { EmptyState } from "@/shared/components/ui/empty-state"
import { UserX } from "lucide-react"

export const dynamic = "force-dynamic"

export default async function StudentLearningPage() {
  const student = await getCurrentStudentUser()
  if (!student) {
    return (
      <div className="space-y-8">
        <EmptyState title="No user found" description="Create a student user to see learning." icon={UserX} />
      </div>
    )
  }

  const [classes, assignments, textbooks] = await Promise.all([
    getStudentClasses(student.id),
    getStudentHomeworkAssignments(student.id),
    getTextbooks(),
  ])

  const now = new Date()
  const pendingCount = assignments.filter((a) => a.progressStatus !== "submitted" && a.progressStatus !== "graded").length
  const dueSoonCount = assignments.filter((a) => {
    if (a.progressStatus === "submitted" || a.progressStatus === "graded") return false
    if (!a.dueAt) return false
    const due = new Date(a.dueAt)
    const in7Days = new Date(now)
    in7Days.setDate(in7Days.getDate() + 7)
    return due >= now && due <= in7Days
  }).length

  const cards = [
    {
      title: "Courses",
      description: "Your enrolled classes.",
      icon: BookOpen,
      href: "/student/learning/courses",
      stat: `${classes.length} enrolled`,
    },
    {
      title: "Assignments",
      description: "Homework and practice.",
      icon: PenTool,
      href: "/student/learning/assignments",
      stat: `${pendingCount} pending${dueSoonCount > 0 ? ` · ${dueSoonCount} due soon` : ""}`,
    },
    {
      title: "Textbooks",
      description: "Browse course materials.",
      icon: Library,
      href: "/student/learning/textbooks",
      stat: `${textbooks.length} available`,
    },
  ]

  return (
    <div className="space-y-8">
      <div>
        <h2 className="text-2xl font-bold tracking-tight">My Learning</h2>
        <p className="text-muted-foreground">Your learning hub: courses, assignments, and textbooks.</p>
      </div>

      <div className="grid gap-6 md:grid-cols-2 lg:grid-cols-3">
        {cards.map((c) => (
          <Link key={c.href} href={c.href}>
            <Card className="h-full transition-all hover:shadow-md hover:border-primary/50">
              <CardHeader className="flex flex-row items-center justify-between space-y-0 pb-2">
                <CardTitle className="text-base font-medium">{c.title}</CardTitle>
                <c.icon className="h-5 w-5 text-muted-foreground" />
              </CardHeader>
              <CardContent className="space-y-2">
                <p className="text-sm text-muted-foreground">{c.description}</p>
                <div className="flex items-center justify-between">
                  <span className="text-sm font-medium">{c.stat}</span>
                  <ArrowRight className="h-4 w-4 text-muted-foreground" />
                </div>
              </CardContent>
            </Card>
          </Link>
        ))}
      </div>
    </div>
  )
}
