import { notFound } from "next/navigation" import Link from "next/link" import { BookOpen, Eye, BookMarked, User, Clock, Layers } from "lucide-react" import { formatViews } from "@/lib/utils" import { GenreBadge } from "@/components/genre-badge" import { StarRating } from "@/components/star-rating" import { ChapterList } from "@/components/chapter-list" import { CommentSection } from "@/components/comment-section" import { NovelDetailActions } from "./novel-detail-actions" import { prisma } from "@/lib/prisma" import connectToMongoDB from "@/lib/mongoose" import { Chapter } from "@/lib/models/chapter" export const dynamic = "force-dynamic" export default async function NovelDetailPage({ params, searchParams }: { params: Promise<{ slug: string }>, searchParams: Promise<{ page?: string }> }) { const { slug } = await params const { page } = await searchParams const currentPage = parseInt(page || "1") const limit = 20 const novel = await prisma.novel.findUnique({ where: { slug }, include: { genres: { include: { genre: true } } } }) if (!novel) { notFound() } // Fetch chapters from MongoDB await connectToMongoDB() const skip = (currentPage - 1) * limit const [chapters, totalChapters] = await Promise.all([ Chapter.find({ novelId: novel.id }) .sort({ number: 1 }) .skip(skip) .limit(limit) .select("id novelId number title createdAt views") .lean(), Chapter.countDocuments({ novelId: novel.id }) ]) const totalPages = Math.ceil(totalChapters / limit) // Convert Mongoose documents to plain objects for Server Component const formattedChapters = chapters.map(c => ({ id: c._id.toString(), novelId: c.novelId, number: c.number, title: c.title, createdAt: (c.createdAt as Date).toISOString(), views: c.views || 0, content: "" // We don't fetch content for the list })) const commentsData = await prisma.comment.findMany({ where: { novelId: novel.id, chapterId: null }, include: { user: true }, orderBy: { createdAt: "desc" } }) // Format explicitly as the CommentProp type const comments = commentsData.map(c => ({ id: c.id, userId: c.user.id, username: c.user.name || "User", avatarColor: c.user.image || "bg-primary", novelId: c.novelId, content: c.content, createdAt: c.createdAt.toISOString().split("T")[0] })) const chapterCommentsData = await prisma.comment.findMany({ where: { novelId: novel.id, chapterId: { not: null } }, include: { user: true }, orderBy: { createdAt: "desc" } }) // Format explicitly as the CommentProp type const chapterComments = chapterCommentsData.map(c => ({ id: c.id, userId: c.user.id, username: c.user.name || "User", avatarColor: c.user.image || "bg-primary", novelId: c.novelId, content: c.content, createdAt: c.createdAt.toISOString().split("T")[0] })) const novelGenres = novel.genres.map(ng => ng.genre) || [] return (
{/* Novel Header */}
{/* Cover */} {novel.title} {/* Info */}

{novel.title}

Tác giả: {novel.authorName} {novel.originalAuthorName && ({novel.originalAuthorName})}
{novel.originalTitle &&
Tên gốc: {novel.originalTitle}
}
Trạng thái: {novel.status}
{novelGenres.map((g, i) => ( {g.name} ))}
{/* Stats Row */}
{novel.totalChapters} Chương
{novel.views} Lượt đọc
{novel.bookmarkCount} Cất giữ
{novel.ratingCount} Đề cử
{/* Description */}

Giới Thiệu

{novel.description}
{/* Chapter list */}

Danh Sách Chương

{/* Comments */}
) }