import type { JSX } from "react"
import { notFound } from "next/navigation"
import { getLessonPlanById } from "@/modules/lesson-preparation/data-access"
import { LessonPlanEditor } from "@/modules/lesson-preparation/components/lesson-plan-editor"
import { getTeacherClasses } from "@/modules/classes/data-access"
import { getAuthContext } from "@/shared/lib/auth-guard"

export const dynamic = "force-dynamic"

export default async function EditLessonPlanPage({
  params,
}: {
  params: Promise<{ planId: string }>
}): Promise<JSX.Element> {
  const { planId } = await params
  const ctx = await getAuthContext()

  const [plan, teacherClasses] = await Promise.all([
    getLessonPlanById(planId, ctx.userId),
    getTeacherClasses({ teacherId: ctx.userId }),
  ])
  if (!plan) notFound()

  const classes = teacherClasses.map((c) => ({ id: c.id, name: c.name }))

  return (
    <div className="h-[calc(100vh-4rem)]">
      <LessonPlanEditor
        planId={plan.id}
        initialTitle={plan.title}
        initialDoc={plan.content}
        textbookId={plan.textbookId ?? undefined}
        chapterId={plan.chapterId ?? undefined}
        classes={classes}
      />
    </div>
  )
}
