"use client"

import type { ReactNode } from "react"
import { useTranslations } from "next-intl"
import {
  TextbookReader,
  type TextbookReaderProps,
} from "@/modules/textbooks/components/textbook-reader"
import type { KnowledgePoint } from "@/modules/textbooks/types"
import { CreateQuestionDialog } from "@/modules/questions/components/create-question-dialog"

/**
 * 教师端 TextbookReader 包装组件。
 *
 * 教师详情页是 Server Component，不能直接向 Client Component（TextbookReader）
 * 传递函数 prop（renderQuestionCreator）。此包装组件在 app 层组装跨模块依赖，
 * 避免 textbooks 模块直接依赖 questions 模块（模块间只通过 data-access 通信）。
 */
export function TeacherTextbookReader({
  chapters,
  textbookId,
}: {
  chapters: TextbookReaderProps["chapters"]
  textbookId: string
}) {
  const t = useTranslations("textbooks")
  const renderQuestionCreator: TextbookReaderProps["renderQuestionCreator"] = ({
    open,
    onOpenChange,
    targetKp,
  }: {
    open: boolean
    onOpenChange: (open: boolean) => void
    targetKp: KnowledgePoint | null
  }): ReactNode => (
    <CreateQuestionDialog
      open={open}
      onOpenChange={onOpenChange}
      defaultKnowledgePointIds={targetKp ? [targetKp.id] : []}
      defaultContent={targetKp ? t("reader.questionCreatorDefaultContent", { name: targetKp.name }) : ""}
      defaultType="text"
    />
  )

  return (
    <TextbookReader
      key={textbookId}
      chapters={chapters}
      textbookId={textbookId}
      renderQuestionCreator={renderQuestionCreator}
    />
  )
}
