import type { Metadata } from "next";
import { getCategories, getNewsroomList } from "@/api/newsroom";
import { PageContainer } from "@/components/layout/PageContainer";
import { SiteFooter } from "@/components/layout/SiteFooter";
import { NewsroomContent } from "@/components/newsroom/NewsroomContent";
import { NewsroomHeader } from "@/components/newsroom/NewsroomHeader";
import { env } from "@/config/env";
export const metadata: Metadata = {
    title: "Newsroom & Insights | Sterling Protection Partners",
    description:
        "Explore our latest announcements, industry insights, and updates from Sterling Protection Partners.",
    openGraph: {
        title: "Newsroom & Insights | Sterling Protection Partners",
        description:
            "Explore our latest announcements, industry insights, and updates from Sterling Protection Partners.",
        type: "website",
        url: "/newsroom",
        images: [
            {
                url: `${env.siteUrl}/images/newsroom/newsroo-hero.png`,
                width: 740,
                height: 479,
                alt: "Newsroom & Insights | Sterling Protection Partners",
            },
        ],
        siteName: "Sterling Protection Partners",
    },
    twitter: {
        card: "summary_large_image",
        title: "Newsroom & Insights | Sterling Protection Partners",
        description:
            "Explore our latest announcements, industry insights, and updates from Sterling Protection Partners.",
        images: [`${env.siteUrl}/images/newsroom/newsroo-hero.png`],
    },
    alternates: {
        canonical: "/newsroom",
    },
    metadataBase: new URL(env.siteUrl),

    publisher: "https://sterlingprotection.co/",
};

export const instant = false;

export default async function NewsroomPage() {
    // Fetch initial data on the server (first 6 news articles)
    const [categories, { pagination, latestRecord, latestRecords, archiveYears }] = await Promise.all([
        getCategories(),
        getNewsroomList({ page: 1, limit: 6, category: "" }),
    ]);

    // Structured Data (JSON-LD) for Blog Collection
    const blogListSchema = {
        "@context": "https://schema.org",
        "@type": "Blog",
        name: "Newsroom & Insights | Sterling Protection Partners",
        description:
            "Explore our latest announcements, industry insights, and updates from Sterling Protection Partners.",
        url: `${env.siteUrl}/newsroom`,
        publisher: {
            "@type": "Organization",
            name: "Sterling Protection Partners",
            url: env.siteUrl,
            logo: {
                "@type": "ImageObject",
                url: `${env.siteUrl}/icon.svg`,
            },
        },
        blogPost: pagination.docs.map((item) => ({
            "@type": "BlogPosting",
            headline: item.t,
            description: item.st || "",
            image: item.bi ? [item.bi] : undefined,
            datePublished: item.d,
            url: `${env.siteUrl}/newsroom/${item.sl || item._id}`,
            author: {
                "@type": "Person",
                name: item.author || item.cat_name || "Sterling Protection Partners",
            },
        })),
    };

    const breadcrumbSchema = {
        "@context": "https://schema.org",
        "@type": "BreadcrumbList",
        itemListElement: [
            {
                "@type": "ListItem",
                position: 1,
                name: "Home",
                item: `${env.siteUrl}/`,
            },
            {
                "@type": "ListItem",
                position: 2,
                name: "Newsroom",
                item: `${env.siteUrl}/newsroom`,
            },
        ],
    };

    return (
        <PageContainer>
            {/* Schema.org Structured Data */}
            <script
                type="application/ld+json"
                dangerouslySetInnerHTML={{ __html: JSON.stringify(blogListSchema) }}
            />
            <script
                type="application/ld+json"
                dangerouslySetInnerHTML={{ __html: JSON.stringify(breadcrumbSchema) }}
            />

            <NewsroomHeader />

            <main className="w-full pt-3 pb-16 sm:pt-5 sm:pb-24">
                <NewsroomContent
                    initialCategories={categories}
                    initialNewsrooms={pagination}
                    initialLatestRecord={latestRecord}
                    initialLatestRecords={latestRecords}
                    initialArchiveYears={archiveYears}
                />
            </main>

            <SiteFooter />
        </PageContainer>
    );
}
