import { CONTACT, getProduct, getTutorials } from "@/lib/content";
import type { Locale } from "@/lib/locales";
import { href, type RouteTarget } from "@/lib/routes";
import { absoluteUrl } from "@/lib/site";

/**
 * Structured data (Blueprint 12): Organization for Sulsys, SoftwareApplication
 * for Nexys System, BreadcrumbList per page, VideoObject on the tutorial
 * library.
 *
 * Only verified facts appear here. There is no `aggregateRating` (no reviews
 * exist), no `offers` (pricing is UNKNOWN), no `foundingDate` (UNKNOWN) and no
 * `numberOfEmployees` (UNKNOWN) — the same discipline as the visible page,
 * because structured data is a claim to a search engine.
 */

type Crumb = { name: string; target: RouteTarget };

export function StructuredData({
  locale,
  crumbs,
  includeProduct = false,
  includeVideos = false,
}: {
  locale: Locale;
  crumbs?: Crumb[];
  includeProduct?: boolean;
  includeVideos?: boolean;
}) {
  const graph: Record<string, unknown>[] = [];

  graph.push({
    "@type": "Organization",
    "@id": absoluteUrl("/#sulsys"),
    name: CONTACT.company,
    url: absoluteUrl(href({ key: "home" }, locale)),
    email: CONTACT.email,
    telephone: CONTACT.phoneE164,
    address: {
      "@type": "PostalAddress",
      streetAddress: CONTACT.street,
      postalCode: CONTACT.postalCode,
      addressLocality: CONTACT.city[locale],
      addressCountry: CONTACT.countryCode,
    },
    sameAs: [CONTACT.facebook, CONTACT.youtube],
  });

  if (includeProduct) {
    const product = getProduct(locale);
    graph.push({
      "@type": "SoftwareApplication",
      "@id": absoluteUrl("/#nexys"),
      name: product.name,
      description: product.oneLiner,
      applicationCategory: "BusinessApplication",
      operatingSystem: "Windows",
      inLanguage: ["sq", "mk"],
      url: absoluteUrl(href({ key: "nexys" }, locale)),
      publisher: { "@id": absoluteUrl("/#sulsys") },
      author: { "@id": absoluteUrl("/#sulsys") },
    });
  }

  if (crumbs && crumbs.length > 0) {
    graph.push({
      "@type": "BreadcrumbList",
      itemListElement: crumbs.map((crumb, index) => ({
        "@type": "ListItem",
        position: index + 1,
        name: crumb.name,
        item: absoluteUrl(href(crumb.target, locale)),
      })),
    });
  }

  if (includeVideos) {
    for (const tutorial of getTutorials()) {
      graph.push({
        "@type": "VideoObject",
        name: tutorial.title,
        uploadDate: tutorial.date.length === 7 ? `${tutorial.date}-01` : tutorial.date,
        embedUrl: `https://www.youtube-nocookie.com/embed/${tutorial.id}`,
        thumbnailUrl: `https://i.ytimg.com/vi/${tutorial.id}/hqdefault.jpg`,
        publisher: { "@id": absoluteUrl("/#sulsys") },
      });
    }
  }

  const json = JSON.stringify({ "@context": "https://schema.org", "@graph": graph });

  return (
    <script
      type="application/ld+json"
      // The payload is built from typed content, never from user input.
      dangerouslySetInnerHTML={{ __html: json }}
    />
  );
}
