import { auth } from "@/lib/auth" import { redirect } from "next/navigation" import { prisma } from "@/lib/prisma" import GuessForm from "@/components/GuessForm" import PhotoImage from "@/components/PhotoImage" import DeletePhotoButton from "@/components/DeletePhotoButton" // Enable caching for this page export const revalidate = 60 // Revalidate every 60 seconds export default async function PhotoPage({ params }: { params: Promise<{ id: string }> }) { const session = await auth() if (!session) { redirect("/login") } const { id } = await params const photo = await prisma.photo.findUnique({ where: { id }, include: { uploader: { select: { name: true, }, }, guesses: { where: { userId: session.user.id, }, orderBy: { createdAt: "desc", }, }, }, }) if (!photo) { return (

Photo not found

) } const userGuess = photo.guesses[0] const hasCorrectGuess = userGuess?.correct || false const isOwner = photo.uploaderId === session.user.id // Type assertion for new fields (penaltyEnabled, penaltyPoints, maxAttempts) type PhotoWithNewFields = typeof photo & { penaltyEnabled: boolean penaltyPoints: number maxAttempts: number | null } const photoWithFields = photo as PhotoWithNewFields // Calculate remaining attempts const userGuessCount = photo.guesses.length const maxAttempts = photoWithFields.maxAttempts ?? null const remainingAttempts = maxAttempts !== null && maxAttempts > 0 ? Math.max(0, maxAttempts - userGuessCount) : null const hasReachedMaxAttempts = maxAttempts !== null && maxAttempts > 0 && userGuessCount >= maxAttempts return (

Guess Who!

{session.user.role === "ADMIN" && ( )}

Uploaded by {photo.uploader.name} on{" "} {new Date(photo.createdAt).toLocaleDateString()}

+{photo.points} {photo.points === 1 ? "point" : "points"} if correct {photoWithFields.penaltyEnabled && photoWithFields.penaltyPoints > 0 && ( -{photoWithFields.penaltyPoints} {photoWithFields.penaltyPoints === 1 ? "point" : "points"} if wrong )} {maxAttempts !== null && maxAttempts > 0 && ( {maxAttempts} {maxAttempts === 1 ? "attempt" : "attempts"} max )}
{hasCorrectGuess ? (

✅ Correct! You guessed it right!

The answer was: {photo.answerName}

You earned {photo.points} {photo.points === 1 ? "point" : "points"}!

) : userGuess ? (

❌ Wrong guess. Try again!

Your last guess: {userGuess.guessText}

{photoWithFields.penaltyEnabled && photoWithFields.penaltyPoints > 0 && (

You lost {photoWithFields.penaltyPoints} {photoWithFields.penaltyPoints === 1 ? "point" : "points"} for this wrong guess.

)}
) : null} {!isOwner && !hasCorrectGuess && remainingAttempts !== null && remainingAttempts > 0 && (

Remaining attempts: {remainingAttempts} of {maxAttempts}

)} {!isOwner && !hasCorrectGuess && hasReachedMaxAttempts && (

⚠️ Maximum attempts reached

You have used all {maxAttempts} {maxAttempts === 1 ? "attempt" : "attempts"} for this photo.

)} {isOwner ? (

📸 This is your photo

You cannot guess on photos you uploaded. The answer is: {photo.answerName}

) : !hasCorrectGuess && !hasReachedMaxAttempts ? ( ) : null}
) }