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

import { BlogSection } from "@/components/BlogSection";
import { BrandsWeServeSection } from "@/components/BrandsWeServeSection";
import { HeroSection } from "@/components/HeroSection";
import { HomeFaqSection } from "@/components/HomeFaqSection";
import { LocationPageActivator } from "@/components/LocationPageActivator";
import { ServicesSection } from "@/components/ServiceCard";
import { HowItWorksSection } from "@/components/StepCard";
import { TestimonialsSection } from "@/components/Testimonials";
import { TrustStatsSection } from "@/components/TrustStatsSection";
import { WhyChooseUsSection } from "@/components/WhyChooseUsSection";
import { getLocationPage, getLocationPages } from "@/lib/location-pages";
import { JsonLd, breadcrumbJsonLd, serviceJsonLd } from "@/lib/seo";

type CityPageProps = {
  params: Promise<{
    locationSlug: string;
  }>;
};

export async function generateMetadata({ params }: CityPageProps): Promise<Metadata> {
  const { locationSlug } = await params;
  const location = await getLocationPage(locationSlug);

  if (!location) {
    return {
      title: "Service Location",
      robots: {
        index: false,
        follow: false,
      },
    };
  }

  return {
    title: `Doorstep Vehicle Service in ${location.name}`,
    description: `Book Ride N Revive doorstep car service, bike service, AC, battery, tyre, and engine support in ${location.name}.`,
    alternates: {
      canonical: `/cities/${location.slug}`,
    },
  };
}

export async function generateStaticParams() {
  const locations = await getLocationPages();

  return locations.map((location) => ({
    locationSlug: location.slug,
  }));
}

export default async function CityLocationPage({ params }: CityPageProps) {
  const { locationSlug } = await params;
  const location = await getLocationPage(locationSlug);

  if (!location) {
    notFound();
  }

  return (
    <>
      <LocationPageActivator locationId={location.id} />
      <JsonLd
        data={[
          serviceJsonLd({
            name: `Doorstep vehicle service in ${location.name}`,
            description: `Ride N Revive doorstep car and bike service support in ${location.name}.`,
            path: `/cities/${location.slug}/`,
            serviceType: "Vehicle service location",
            areaServed: location.name,
          }),
          breadcrumbJsonLd([
            { name: "Home", path: "/" },
            { name: "Cities", path: "/cities/" },
            { name: location.name, path: `/cities/${location.slug}/` },
          ]),
        ]}
      />
      <HeroSection />
      <ServicesSection />
      <HowItWorksSection />
      <WhyChooseUsSection />
      <TrustStatsSection />
      <HomeFaqSection />
      <TestimonialsSection />
      <BlogSection />
      <BrandsWeServeSection />
    </>
  );
}
