import type { Metadata } from "next";
import Hero from "@/components/Hero";
import About from "@/components/About";
import Services from "@/components/Services";
import Technologies from "@/components/Technologies";
import Projects from "@/components/Projects";
import Training from "@/components/Training";
import Clients from "@/components/Clients";
import CallToAction from "@/components/CallToAction";
import InspectionMarketplaceLanding from "@/components/InspectionMarketplaceLanding";
import {
  getHomePageContent,
  getHomePageSeo,
  getHomeServicesServer,
  getHomeTechnologiesServer,
  getHomeProjectsServer,
  getHomeTrainingsServer,
  getHomeClientsServer,
} from "@/services/pages/public/home";

export const revalidate = 1800;

export async function generateMetadata(): Promise<Metadata> {
  const seo = await getHomePageSeo();

  return {
    ...(seo?.meta_title ? { title: seo.meta_title } : {}),
    ...(seo?.meta_description ? { description: seo.meta_description } : {}),
    ...(seo?.no_index ? { robots: { index: false, follow: false } } : {}),
    ...(seo?.canonical_url ? { alternates: { canonical: seo.canonical_url } } : {}),
    openGraph: {
      ...(seo?.og_title ? { title: seo.og_title } : {}),
      ...(seo?.og_description ? { description: seo.og_description } : {}),
      ...(seo?.og_image_url ? { images: [{ url: seo.og_image_url }] } : {}),
    },
  };
}

const Index = async () => {
  const [seo, api, services, technologies, projects, trainings, clients] =
    await Promise.all([
      getHomePageSeo(),
      getHomePageContent(),
      getHomeServicesServer(),
      getHomeTechnologiesServer(),
      getHomeProjectsServer(),
      getHomeTrainingsServer(),
      getHomeClientsServer(),
    ]);

  const schemaJson = seo?.schema_json
    ? typeof seo.schema_json === "object"
      ? JSON.stringify(seo.schema_json)
      : seo.schema_json
    : null;

  const hero = api?.hero ?? {};
  const about = api?.about ?? {};
  const sh = api?.section_headings ?? {};
  const cta = api?.cta ?? {};

  return (
    <>
      {schemaJson && (
        <script
          type="application/ld+json"
          dangerouslySetInnerHTML={{ __html: schemaJson }}
        />
      )}
      <div className="bg-background text-foreground overflow-x-hidden">
      <Hero
        heading={hero.heading || undefined}
        headingHighlight={hero.heading_highlight || undefined}
        subtitle={hero.subtitle || undefined}
        ctaPrimaryLabel={hero.cta_primary?.label || undefined}
        ctaPrimaryUrl={hero.cta_primary?.url || undefined}
        ctaSecondaryLabel={hero.cta_secondary?.label || undefined}
        ctaSecondaryUrl={hero.cta_secondary?.url || undefined}
        videoUrl={hero.video_url || undefined}
        posterUrl={hero.poster_url || undefined}
      />
      <About
        heading={about.heading || undefined}
        body={about.body || undefined}
        badgeValue={about.badge?.value || undefined}
        badgeLabel={about.badge?.label || undefined}
        stat1Title={about.stats?.[0]?.title || undefined}
        stat1Desc={about.stats?.[0]?.desc || undefined}
        stat2Title={about.stats?.[1]?.title || undefined}
        stat2Desc={about.stats?.[1]?.desc || undefined}
        imageUrl={about.image_url || undefined}
      />
      <Services
        heading={sh.services?.heading || undefined}
        subtitle={sh.services?.subtitle || undefined}
        services={services}
      />
      <Technologies
        heading={sh.technologies?.heading || undefined}
        subtitle={sh.technologies?.subtitle || undefined}
        technologies={technologies}
      />
      <Projects
        heading={sh.projects?.heading || undefined}
        subtitle={sh.projects?.subtitle || undefined}
        projects={projects}
      />
      <Training
        heading={sh.training?.heading || undefined}
        subtitle={sh.training?.subtitle || undefined}
        imageUrl={sh.training?.image_url || undefined}
        trainings={trainings}
      />
      <Clients
        heading={sh.clients?.heading || undefined}
        subtitle={sh.clients?.subtitle || undefined}
        clients={clients}
      />
      <InspectionMarketplaceLanding />
      <CallToAction
        heading={cta.heading || undefined}
        subtitle={cta.subtitle || undefined}
        buttonLabel={cta.button_label || undefined}
      />
    </div>
    </>
  );
};

export default Index;

