import { notFound } from "next/navigation"
import { ChapterContent } from "@/components/chapter-content"
import { getSectionBySlug, getChapterBySectionSlug } from "@/lib/book-file-system"
import { specialSections } from "@/lib/book-data"

interface ReadPageProps {
  params: Promise<{ id: string }>
}

export const dynamic = "force-dynamic"
export const runtime = "nodejs"

export default async function ReadPage({ params }: ReadPageProps) {
  const { id } = await params

  if (id === "preface") {
    return <ChapterContent section={specialSections.preface} partTitle="序言" chapterTitle="" />
  }

  if (id === "epilogue") {
    return <ChapterContent section={specialSections.epilogue} partTitle="尾声" chapterTitle="" />
  }

  try {
    const section = getSectionBySlug(id)
    if (!section) {
      notFound()
    }

    const context = getChapterBySectionSlug(id)
    if (!context) {
      notFound()
    }

    return <ChapterContent section={section} partTitle={context.part.title} chapterTitle={context.chapter.title} />
  } catch (error) {
    console.error("[v0] Error in ReadPage:", error)
    notFound()
  }
}
