import type { Metadata } from "next";
import { notFound } from "next/navigation";

import JsonLd from "@/components/common/JsonLd";
import AudienceSection from "@/components/home/AudienceSection";
import CTASection from "@/components/home/CTASection";
import FAQSection from "@/components/home/FAQSection";
import FeaturesSection from "@/components/home/FeaturesSection";
import GrowthSection from "@/components/home/GrowthSection";
import HeroSection from "@/components/home/HeroSection";
import HowItWorks from "@/components/home/HowItWorks";
import PlatformDiagram from "@/components/home/PlatformDiagram";
import ProductsSection from "@/components/home/ProductsSection";
import TestimonialsSection from "@/components/home/TestimonialsSection";
import { siteConfig } from "@/config/site.config";
import { getHomeData } from "@/data/home.data";
import { getProducts } from "@/data/products.data";
import { getDictionary } from "@/lib/i18n";
import { hasLocale } from "@/lib/locales";
import { createMetadata } from "@/lib/seo";

export async function generateMetadata({
  params,
}: PageProps<"/[lang]">): Promise<Metadata> {
  const { lang } = await params;
  if (!hasLocale(lang)) notFound();
  const dictionary = await getDictionary(lang);
  return createMetadata({
    locale: lang,
    title: dictionary.home.badge,
    description: dictionary.home.heroDescription,
  });
}

export default async function HomePage({ params }: PageProps<"/[lang]">) {
  const { lang } = await params;
  if (!hasLocale(lang)) notFound();

  const dictionary = await getDictionary(lang);
  const homeData = getHomeData(dictionary);
  const products = getProducts(dictionary, lang);
  const websiteJsonLd = {
    "@context": "https://schema.org",
    "@type": "WebSite",
    name: "PureSaaS",
    url: siteConfig.url,
    inLanguage: lang,
    potentialAction: {
      "@type": "SearchAction",
      target: `${siteConfig.url}/${lang}/blogs?search={search_term_string}`,
      "query-input": "required name=search_term_string",
    },
  };
  const faqJsonLd = {
    "@context": "https://schema.org",
    "@type": "FAQPage",
    mainEntity: dictionary.faqs.map(([question, answer]) => ({
      "@type": "Question",
      name: question,
      acceptedAnswer: { "@type": "Answer", text: answer },
    })),
  };

  return (
    <>
      <JsonLd data={[websiteJsonLd, faqJsonLd]} />
      <HeroSection dictionary={dictionary} locale={lang} />
      <ProductsSection
        exploreLabel={dictionary.common.explore}
        locale={lang}
        products={products}
        subtitle={dictionary.home.productsSubtitle}
        title={dictionary.home.productsTitle}
      />
      <FeaturesSection features={homeData.features} title={dictionary.home.featuresTitle} />
      <AudienceSection
        audiences={homeData.audiences}
        subtitle={dictionary.home.audienceSubtitle}
        title={dictionary.home.audienceTitle}
      />
      <PlatformDiagram
        platformLabel={dictionary.home.diagramPlatform}
        subtitle={dictionary.home.diagramSubtitle}
        title={dictionary.home.diagramTitle}
      />
      <HowItWorks
        steps={dictionary.steps}
        subtitle={dictionary.home.howSubtitle}
        title={dictionary.home.howTitle}
      />
      <GrowthSection dictionary={dictionary} />
      <TestimonialsSection
        subtitle={dictionary.home.testimonialsSubtitle}
        testimonials={homeData.testimonials}
        title={dictionary.home.testimonialsTitle}
        trustCards={homeData.trustCards}
      />
      <FAQSection
        faqs={dictionary.faqs}
        locale={lang}
        subtitle={dictionary.home.faqSubtitle}
        title={dictionary.home.faqTitle}
      />
      <CTASection
        bookDemo={dictionary.common.bookDemo}
        description={dictionary.home.ctaDescription}
        locale={lang}
        talkSales={dictionary.common.talkSales}
        title={dictionary.home.ctaTitle}
      />
    </>
  );
}
