import type { JSX } from "react"
import { notFound } from "next/navigation"
import { getTranslations } from "next-intl/server"
import { getHomeworkAssignmentById } from "@/modules/homework/data-access"
import { getStudentSubmissionResult } from "@/modules/homework/data-access-student"
import { HomeworkSubmissionResult } from "@/modules/homework/components/homework-submission-result"
import { getSession } from "@/shared/lib/session"

export const dynamic = "force-dynamic"

export default async function HomeworkResultPage({ params }: { params: Promise<{ assignmentId: string }> }): Promise<JSX.Element> {
  const { assignmentId } = await params
  const t = await getTranslations("examHomework")
  const session = await getSession()
  const studentId = session?.user?.id

  if (!studentId) return notFound()

  const [assignment, submission] = await Promise.all([
    getHomeworkAssignmentById(assignmentId),
    getStudentSubmissionResult(assignmentId, studentId),
  ])

  if (!assignment) return notFound()
  if (!submission) return notFound()

  return (
    <div className="flex h-full flex-col space-y-6 p-8">
      <div>
        <h1 className="text-2xl font-bold tracking-tight">{t("homework.result.title")}</h1>
        <p className="text-muted-foreground">{assignment.title}</p>
      </div>
      <HomeworkSubmissionResult submission={submission} />
    </div>
  )
}
