import { site } from "@data/site";
import { assetUrl, canonical, siteUrl } from "./url";

type Crumb = { name: string; href: string };

export function organizationSchema() {
  return {
    "@context": "https://schema.org",
    "@type": "Organization",
    "@id": `${siteUrl}/#organization`,
    name: site.name,
    legalName: site.legalName,
    url: siteUrl,
    logo: assetUrl("/assets/bluecap-logo.png"),
    email: site.email,
    telephone: site.phone,
    founder: {
      "@type": "Person",
      name: site.founder,
    },
    description: site.description,
  };
}

export function websiteSchema() {
  return {
    "@context": "https://schema.org",
    "@type": "WebSite",
    "@id": `${siteUrl}/#website`,
    url: siteUrl,
    name: site.name,
    publisher: { "@id": `${siteUrl}/#organization` },
  };
}

export function pageSchema({
  type = "WebPage",
  title,
  description,
  pathname,
}: {
  type?: "WebPage" | "AboutPage" | "ContactPage";
  title: string;
  description: string;
  pathname: string;
}) {
  return {
    "@context": "https://schema.org",
    "@type": type,
    "@id": `${canonical(pathname)}#webpage`,
    url: canonical(pathname),
    name: title,
    description,
    isPartOf: { "@id": `${siteUrl}/#website` },
    about: { "@id": `${siteUrl}/#organization` },
  };
}

export function serviceSchema({
  title,
  description,
  pathname,
}: {
  title: string;
  description: string;
  pathname: string;
}) {
  return {
    "@context": "https://schema.org",
    "@type": "Service",
    "@id": `${canonical(pathname)}#service`,
    name: title,
    description,
    provider: { "@id": `${siteUrl}/#organization` },
    areaServed: "United States",
    url: canonical(pathname),
    serviceType: "Water-sector advisory services",
  };
}

export function breadcrumbSchema(crumbs: Crumb[]) {
  return {
    "@context": "https://schema.org",
    "@type": "BreadcrumbList",
    itemListElement: crumbs.map((crumb, index) => ({
      "@type": "ListItem",
      position: index + 1,
      name: crumb.name,
      item: canonical(crumb.href),
    })),
  };
}
