"use client"; import { useState, useEffect } from "react"; import { useRouter } from "next/navigation"; interface Webinar { id: string; title: string; description: string; speaker: string; startAt: string; duration: number; priceCents: number; visibility: "PUBLIC" | "PRIVATE"; isActive: boolean; capacity: number; learningPoints?: string[]; _count?: { registrations: number }; } interface WebinarDetailClientProps { id: string; } export default function WebinarDetailClient({ id }: WebinarDetailClientProps) { const [webinar, setWebinar] = useState(null); const [loading, setLoading] = useState(true); const [registering, setRegistering] = useState(false); const [isAuthenticated, setIsAuthenticated] = useState(false); const [showAuthModal, setShowAuthModal] = useState(false); const router = useRouter(); useEffect(() => { fetchWebinar(); checkAuth(); }, [id]); const checkAuth = async () => { try { const response = await fetch("/api/auth/me"); setIsAuthenticated(response.ok); } catch (error) { setIsAuthenticated(false); } }; const fetchWebinar = async () => { try { const response = await fetch(`/api/webinars/${id}`); if (response.ok) { const data = await response.json(); setWebinar(data.webinar); } else { router.push("/webinars"); } } catch (error) { console.error("Failed to fetch webinar:", error); router.push("/webinars"); } finally { setLoading(false); } }; const handleRegister = async () => { if (!isAuthenticated) { setShowAuthModal(true); return; } setRegistering(true); try { const response = await fetch("/api/registrations", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ webinarId: id }), }); if (response.ok) { alert("Successfully registered for the webinar!"); router.push("/account/webinars"); } else { const data = await response.json(); alert(data.error || "Registration failed"); } } catch (error) { alert("Failed to register. Please try again."); } finally { setRegistering(false); } }; if (loading) { return (

Loading webinar details...

); } if (!webinar) { return null; } const webinarDate = new Date(webinar.startAt); const registeredCount = webinar._count?.registrations || 0; const spotsLeft = webinar.capacity - registeredCount; const isFull = spotsLeft <= 0; return (
{webinar.isActive ? "ACTIVE" : "INACTIVE"} {isFull && ( FULL )} {webinar.visibility === "PRIVATE" ? "PRIVATE" : "PUBLIC"}

{webinar.title}

{webinar.description}

📅 Date & Time

{webinarDate.toLocaleDateString("en-US", { weekday: "long", year: "numeric", month: "long", day: "numeric", })}

{webinarDate.toLocaleTimeString("en-US", { hour: "2-digit", minute: "2-digit", timeZoneName: "short", })}

👨‍🏫 Instructor

{webinar.speaker}

Estate planning specialist

⏱️ Duration

{webinar.duration} minutes

Interactive Q&A included

💰 Price

{webinar.priceCents === 0 ? "FREE" : `$${(webinar.priceCents / 100).toFixed(2)}`}

Secure checkout

{webinar.learningPoints && webinar.learningPoints.length > 0 && (

📚 What You'll Learn

    {webinar.learningPoints.map((point, index) => (
  • {point}
  • ))}
)}

Registration

{isFull ? "This webinar is fully booked." : `${spotsLeft} spots left out of ${webinar.capacity}`}

{showAuthModal && (

Sign in required

Please sign in to register for this webinar.

)}
); }