import type { Metadata } from "next";
import { prisma } from "@/lib/prisma";
import VolunteerForm from "./VolunteerForm";
import AnimatedCounter from "@/components/AnimatedCounter";
import { notFound } from "next/navigation";

export async function generateMetadata({ params }: { params: Promise<{ id: string }> }): Promise<Metadata> {
    const { id } = await params;
    const opp = await prisma.volunteerOpportunity.findUnique({ where: { id } });

    if (!opp) return {};

    return {
        title: opp.title,
        description: opp.description || `Volunteer opportunity with the South Forsyth Middle School PTO: ${opp.title}.`,
    };
}

export default async function VolunteerDetailPage({ params }: { params: Promise<{ id: string }> }) {
    const { id } = await params;
    const opp = await prisma.volunteerOpportunity.findUnique({
        where: { id },
        include: { customFields: true }
    });

    if (!opp) return notFound();

    const signupCount = await prisma.volunteerSignup.count({ where: { opportunityId: opp.id } });

    const eventJsonLd = {
        "@context": "https://schema.org",
        "@type": "Event",
        name: opp.title,
        description: opp.description || undefined,
        startDate: opp.date ? new Date(opp.date).toISOString() : undefined,
        location: opp.location
            ? { "@type": "Place", name: opp.location }
            : undefined,
        organizer: {
            "@type": "Organization",
            name: "South Forsyth Middle School PTO",
        },
    };

    return (
        <div className="container" style={{ padding: "4rem 1.5rem", maxWidth: "800px" }}>
            <script
                type="application/ld+json"
                dangerouslySetInnerHTML={{ __html: JSON.stringify(eventJsonLd) }}
            />
            <a href="/volunteer" style={{ color: "var(--primary)", display: "inline-block", marginBottom: "2rem", fontWeight: 600 }}>
                &larr; Back to Opportunities
            </a>

            <div className="glass-panel animate-fade-in" style={{ padding: "3rem" }}>
                <h1 style={{ fontSize: "2.5rem", marginBottom: "1rem" }}>{opp.title}</h1>
                <p style={{ color: "var(--text-muted)", fontSize: "1.1rem", marginBottom: "2rem" }}>{opp.description}</p>

                <div style={{ display: "flex", gap: "2rem", marginBottom: "3rem", paddingBottom: "2rem", borderBottom: "1px solid var(--border)" }}>
                    {opp.date && (
                        <div>
                            <div style={{ fontSize: "0.85rem", textTransform: "uppercase", fontWeight: 700, color: "var(--text-muted)" }}>Date</div>
                            <div style={{ fontSize: "1.1rem", fontWeight: 500 }}>{new Date(opp.date).toLocaleDateString()}</div>
                        </div>
                    )}
                    {opp.location && (
                        <div>
                            <div style={{ fontSize: "0.85rem", textTransform: "uppercase", fontWeight: 700, color: "var(--text-muted)" }}>Location</div>
                            <div style={{ fontSize: "1.1rem", fontWeight: 500 }}>{opp.location}</div>
                        </div>
                    )}
                    {signupCount > 0 && (
                        <div>
                            <div style={{ fontSize: "0.85rem", textTransform: "uppercase", fontWeight: 700, color: "var(--text-muted)" }}>Signed Up</div>
                            <div style={{ fontSize: "1.1rem", fontWeight: 500, color: "#065f46" }}>
                                <AnimatedCounter value={signupCount} /> {signupCount === 1 ? "parent" : "parents"} already
                            </div>
                        </div>
                    )}
                </div>

                <h2 style={{ fontSize: "1.5rem", marginBottom: "1.5rem" }}>Sign Up Form</h2>
                <VolunteerForm opportunityId={opp.id} customFields={opp.customFields} />
            </div>
        </div>
    );
}
