import { Metadata } from 'next';
import { getAllProductCards } from '@/data/products';
import { getAllCategories } from '@/lib/db/categories';
import { ARTICLES } from '@/data/articles';
import { generateOrganizationSchema, generateCollectionSchema, jsonLdHtml } from '@/lib/seo';
import { HomeHeroSection } from '@/components/sections/HomeHeroSection';
import { HomeStatsSection } from '@/components/sections/HomeStatsSection';
import { HomeCategoryGridSection } from '@/components/sections/HomeCategoryGridSection';
import { HomeTrendingSection } from '@/components/sections/HomeTrendingSection';
import { HomePromoBannerSection } from '@/components/sections/HomePromoBannerSection';
import { HomeFeaturedSection } from '@/components/sections/HomeFeaturedSection';
import { HomeTestimonialsSection } from '@/components/sections/HomeTestimonialsSection';
import { HomeTechInsightsSection } from '@/components/sections/HomeTechInsightsSection';
import { HomeNewsletterSection } from '@/components/sections/HomeNewsletterSection';
import { HomeShopeeSection } from '@/components/sections/HomeShopeeSection';

import { homeMetadata } from '@/lib/metadata';
import { SITE_CONFIG } from '@/lib/constants';

export const metadata: Metadata = {
  title: homeMetadata.title,
  description: homeMetadata.description,
  alternates: homeMetadata.alternates,
  openGraph: {
    title: SITE_CONFIG.fullName,
    description: SITE_CONFIG.description,
    url: SITE_CONFIG.url,
    type: 'website',
    images: [
      {
        url: '/og-main.webp',
        width: 1200,
        height: 630,
      },
    ],
  },
};

import { HomeAllProductsSection } from '@/components/sections/HomeAllProductsSection';

// Refresh the statically-rendered homepage periodically to pick up DB changes.
export const revalidate = 300;

export default async function HomePage() {
  const [products, categories] = await Promise.all([getAllProductCards(), getAllCategories()]);
  const organizationSchema = generateOrganizationSchema();
  const collectionSchema = generateCollectionSchema('Produk Unggulan', products, '/');

  return (
    <>
      <script
        type="application/ld+json"
        dangerouslySetInnerHTML={{ __html: jsonLdHtml(organizationSchema) }}
      />
      <script
        type="application/ld+json"
        dangerouslySetInnerHTML={{ __html: jsonLdHtml(collectionSchema) }}
      />

      <HomeHeroSection />
      <HomeStatsSection />
      <HomeCategoryGridSection categories={categories} />
      <HomeTrendingSection products={products} />
      <HomePromoBannerSection />
      <HomeShopeeSection />
      <HomeFeaturedSection products={products.slice(2)} />
      <HomeTestimonialsSection />
      <HomeAllProductsSection products={products} />
      <HomeTechInsightsSection articles={ARTICLES} />
      <HomeNewsletterSection />
    </>
  );
}
