import type { Metadata } from "next";
import { ContactSection } from "@/components/site/ContactSection";
import { prisma } from "@/lib/prisma";
import { siteUrl } from "@/lib/utils";

function seoKeywords(value?: string | null) {
  return value?.split(",").map((keyword) => keyword.trim()).filter(Boolean);
}

export async function generateMetadata(): Promise<Metadata> {
  const page = await prisma.pageContent.findUnique({ where: { key: "CONTACT" } }).catch(() => null);

  return {
    title: page?.seoTitle ?? page?.title ?? "Contact",
    description: page?.seoDescription ?? page?.subtitle ?? "Contact Just Chill Nepal for collaborations, suggestions, and sponsorships.",
    keywords: seoKeywords(page?.seoKeywords),
    alternates: { canonical: siteUrl("/contact") },
    openGraph: {
      title: page?.seoTitle ?? page?.title ?? "Contact Just Chill Nepal",
      description: page?.seoDescription ?? page?.subtitle ?? "Contact Just Chill Nepal for collaborations, suggestions, and sponsorships.",
      images: page?.heroImage ? [{ url: page.heroImage, alt: page.title }] : undefined,
      url: siteUrl("/contact"),
      type: "website"
    },
    twitter: {
      card: page?.heroImage ? "summary_large_image" : "summary",
      title: page?.seoTitle ?? page?.title ?? "Contact Just Chill Nepal",
      description: page?.seoDescription ?? page?.subtitle ?? "Contact Just Chill Nepal for collaborations, suggestions, and sponsorships.",
      images: page?.heroImage ? [page.heroImage] : undefined
    }
  };
}

type ContactPageProps = {
  searchParams: Promise<{ contact?: string }>;
};

export default async function ContactPage({ searchParams }: ContactPageProps) {
  const params = await searchParams;
  const contact = await prisma.contactSetting.findUnique({ where: { key: "contact" } });

  return (
    <main className="container" style={{ padding: "4rem 1.25rem" }}>
      <ContactSection contact={contact} showSuccess={params.contact === "sent"} />
    </main>
  );
}
