"use client";
import { usePathname } from "next/navigation";

export default function BreadcrumbSchema() {
  const pathname = usePathname();

  // Skip on home page (no breadcrumb needed for /)
  if (pathname === "/" || pathname === "") return null;

  const segments = pathname.split("/").filter(Boolean);
  const baseUrl = "https://www.fruxinfo.com";

  // Build breadcrumb items: Home → segment1 → segment2 → ...
  const itemListElement = [
    {
      "@type": "ListItem",
      position: 1,
      name: "Home",
      item: baseUrl + "/",
    },
    ...segments.map((seg, i) => {
      // Convert slug to readable name: "case-study-acemica" → "Case Study Acemica"
      const name = seg
        .split("-")
        .map((w) => w.charAt(0).toUpperCase() + w.slice(1))
        .join(" ");
      const url = baseUrl + "/" + segments.slice(0, i + 1).join("/");
      return {
        "@type": "ListItem",
        position: i + 2,
        name,
        item: url,
      };
    }),
  ];

  const schema = {
    "@context": "https://schema.org",
    "@type": "BreadcrumbList",
    itemListElement,
  };

  return (
    <script
      type="application/ld+json"
      dangerouslySetInnerHTML={{ __html: JSON.stringify(schema) }}
    />
  );
}
