"use client";

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

import { breadcrumbSchema, metadataTitle, pageSchema } from "@/lib/schema";
import { SITE_METADATA } from "@/lib/seo";
import config from "@/utils/config";

import JsonLd from "./JsonLd";

export default function PageSchema({
  title = SITE_METADATA.title,
  description = SITE_METADATA.description,
  breadcrumb = false,
}: {
  title?: Metadata["title"];
  description?: string | null;
  breadcrumb?: boolean;
}) {
  const pathname = usePathname();
  const siteUrl = config.ENV_VARS.UI_BASE_URL || "https://rebates.com";
  const name = metadataTitle(title);

  return (
    <>
      <JsonLd data={pageSchema({ siteUrl, pathname, title, description })} />
      {breadcrumb ? (
        <JsonLd data={breadcrumbSchema(
          pathname === "/"
            ? [{ label: "Home", href: "/" }]
            : [{ label: "Home", href: "/" }, { label: name }],
          pathname,
          siteUrl,
        )} />
      ) : null}
    </>
  );
}
