import { notFound } from "next/navigation"; import { getServerSession } from "next-auth"; import Link from "next/link"; import { authOptions } from "@/lib/auth"; import { tripService } from "@/server/services/trip.service"; import { formatRupiah, formatDateRange } from "@/lib/utils"; import { JoinTripButton } from "@/features/trip/components/join-trip-button"; import { ImageGallery } from "@/features/trip/components/image-gallery"; import { TripReviewSection } from "@/features/review/components/trip-review-section"; import { isPastTripLastDayForReview, isTripDepartureDayPast, } from "@/lib/trip-dates"; export default async function TripDetailPage({ params, }: { params: Promise<{ id: string }>; }) { const { id } = await params; const session = await getServerSession(authOptions); let trip; try { trip = await tripService.getTripById(id); } catch { notFound(); } const activeParticipants = trip.participants.filter( (p) => p.status !== "CANCELLED" ); const participantCount = activeParticipants.length; const spotsLeft = trip.maxParticipants - participantCount; const fillPercent = Math.min( (participantCount / trip.maxParticipants) * 100, 100 ); const isOrganizer = session?.user?.id === trip.organizerId; const currentParticipation = session?.user ? trip.participants.find( (p) => p.userId === session.user.id && p.status !== "CANCELLED" ) : null; const isDeparturePast = isTripDepartureDayPast(trip.date); const canReview = !!session?.user && !isOrganizer && currentParticipation?.status === "CONFIRMED" && isPastTripLastDayForReview(trip.date, trip.endDate); const myReview = session?.user ? trip.reviews.find((r) => r.userId === session.user.id) ?? null : null; const averageRating = trip.reviews.length > 0 ? Math.round( (trip.reviews.reduce((s, r) => s + r.rating, 0) / trip.reviews.length) * 10 ) / 10 : null; return (
{/* Breadcrumb */}
Open Trip / {trip.mountain}
{/* Image Gallery */} {/* Title bar */}

{trip.title}

🏔️ {trip.mountain}

{trip.status}
{/* Info Grid */}
📍

Lokasi

{trip.location}

📅

Tanggal

{formatDateRange(trip.date, trip.endDate)}

💰

Harga

{formatRupiah(trip.price)}

👤

Organizer

{trip.organizer.name}

{/* Participant Progress */}
Peserta {participantCount}{" "} / {trip.maxParticipants}
= 100 ? "bg-amber-500" : fillPercent >= 70 ? "bg-secondary-500" : "bg-primary-500" }`} style={{ width: `${fillPercent}%` }} />

{spotsLeft > 0 ? `${spotsLeft} slot tersisa — yuk gabung!` : "Trip sudah penuh"}

{/* Description */} {trip.description && (

Deskripsi Trip

{trip.description}

)} {/* Action */} ({ id: r.id, rating: r.rating, comment: r.comment, createdAt: r.createdAt, user: r.user, }))} averageRating={averageRating} canReview={canReview} myReview={ myReview ? { rating: myReview.rating, comment: myReview.comment } : null } /> {/* Participants List */}

Peserta ({participantCount})

{participantCount === 0 ? (

Belum ada peserta. Jadilah yang pertama! 🎒

) : (
{activeParticipants.map((p) => (
{p.user.name.charAt(0).toUpperCase()}
{p.user.name}
))}
)}
); }