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

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

export const dynamic = 'force-dynamic';

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

  // Check special sections first
  // Note: Special sections might not have file paths in the dynamic system yet unless mapped
  // For now, we keep the hardcoded specialSections check but maybe we should map them to files too
  if (id === "preface") {
    return <ChapterContent section={specialSections.preface} partTitle="序言" chapterTitle="" />
  }

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

  // Find regular section from dynamic file system
  const section = getSectionBySlug(id)
  if (!section) {
    notFound()
  }

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

  // Read content from file
  const content = getSectionContent(section.filePath)
  const sectionWithContent = { ...section, content }

  return <ChapterContent section={sectionWithContent} partTitle={context.part.title} chapterTitle={context.chapter.title} />
}
