import type { Metadata } from "next";
import { Suspense } from "react";
import { serverApiBaseUrl } from "@/lib/api-url";
import { JsonLd, breadcrumbJsonLd, serviceJsonLd } from "@/lib/seo";
import { CarAcCompaniesClient, type CarCompany } from "./CarAcCompaniesClient";

export const metadata: Metadata = {
  title: "Car AC Service Companies",
  description:
    "Choose your car company for doorstep car AC service, cooling checks, gas refill, leakage tests, and compressor support from Ride N Revive.",
  alternates: {
    canonical: "/services/car-ac-service",
  },
  openGraph: {
    title: "Car AC Service Companies | Ride N Revive",
    description:
      "Browse supported car companies and book doorstep car AC service with Ride N Revive.",
    url: "/services/car-ac-service",
  },
};

type CompaniesResponse = {
  logos?: CarCompany[];
  error?: string;
};

export default async function CarAcServicePage() {
  let initialCompanies: CarCompany[] | undefined;
  let initialErrorMessage = "";

  try {
    const response = await fetch(`${serverApiBaseUrl}/api/vehicle-logos?vehicleType=car`, {
      cache: "no-store",
    });
    const data = (await response.json().catch(() => ({}))) as CompaniesResponse;

    if (!response.ok) {
      throw new Error(data.error ?? "Could not load car companies");
    }

    initialCompanies = data.logos ?? [];
  } catch (error) {
    initialErrorMessage = error instanceof Error ? error.message : "Could not load car companies";
  }

  return (
    <>
      <JsonLd
        data={[
          serviceJsonLd({
            name: "Doorstep Car AC Service",
            description:
              "Choose your car company for doorstep car AC service, cooling checks, gas refill, leakage tests, and compressor support from Ride N Revive.",
            path: "/services/car-ac-service/",
            serviceType: "Car AC service",
          }),
          breadcrumbJsonLd([
            { name: "Home", path: "/" },
            { name: "Services", path: "/services/" },
            { name: "Car AC Service", path: "/services/car-ac-service/" },
          ]),
        ]}
      />
      <Suspense fallback={null}>
        <CarAcCompaniesClient
          initialCompanies={initialCompanies}
          initialErrorMessage={initialErrorMessage}
        />
      </Suspense>
    </>
  );
}
