// app/[city]/[category]/page.tsx

import type { Metadata } from "next";
import HomeClient from "./HomeClient";
import Script from "next/script";
import { notFound } from "next/navigation";
// import { getOrganizationRating } from "@/api/ratingApi";

const formatCity = (slug?: string) => {
  if (!slug) return "";
  return slug.replace(/-/g, " ").replace(/\b\w/g, (c) => c.toUpperCase());
};

export async function generateMetadata(
  { params }: { params: Promise<{ city: string; category: string }> }
): Promise<Metadata> {

  const { city: citySlug, category: categorySlug } = await params; // ✅ FIX

  if (!citySlug || !categorySlug) {
    return {
      title: "Ridoo – Rent Self Drive Bikes & Cars",
      description: "Book bikes and cars easily with Ridoo.",
    };
  }

  const city = citySlug
    .replace(/-/g, " ")
    .replace(/\b\w/g, (c: string) => c.toUpperCase());

  const vehicle =
    categorySlug === "bike-rentals" ? "Bikes" : "Cars";

  const title = `Ridoo - Rent Self Drive ${vehicle} in ${city}`;
  const description = `Rent self-drive ${vehicle.toLowerCase()} in ${city} with Ridoo. Affordable pricing, flexible plans, and instant booking.`;


  const validCategories = [
  "bike-rentals",
  "car-rentals",
  "select",
];

if (!validCategories.includes(categorySlug)) {
  notFound();
}

  return {
    title,
    description,
    alternates: {
      canonical: `/${citySlug}/${categorySlug}`,
    },
    openGraph: {
      title,
      description,
      url: `/${citySlug}/${categorySlug}`,
      siteName: "Ridoo",
      type: "website",
    },
  };
}

export default async function Page({
  params,
  ...rest
}: {
  params: Promise<{ city: string; category: string }>;
}) {

  const { city: citySlug, category: categorySlug } = await params;

  const city = formatCity(citySlug);
  // const ratingData = await getOrganizationRating();

  return (
    <>
      {/* SERVICE SCHEMA */}
      {/* <Script
  id="service-schema"
  type="application/ld+json"
  dangerouslySetInnerHTML={{
    __html: JSON.stringify({
      "@context": "https://schema.org",

      "@type": "Service",

      name:
        categorySlug === "bike-rentals"
          ? `Bike Rentals in ${city}`
          : `Car Rentals in ${city}`,

      provider: {
        "@type": "Organization",
        name: "Ridoo",
      },

      areaServed: city,

      serviceType:
        categorySlug === "bike-rentals"
          ? "Bike Rental"
          : "Car Rental",

      aggregateRating: {
        "@type": "AggregateRating",

        ratingValue:
          ratingData?.rating_value || 4.5,

        reviewCount:
          ratingData?.rating_count || 10,
      },
    }),
  }}
/> */}

      <HomeClient
        params={{
          city: citySlug,
          category: categorySlug,
        }}
        {...rest}
      />
    </>
  );
}