import {
  absoluteUrl,
  businessProducts,
  entityPages,
  machineReadableFiles,
  serviceEntities,
  siteConfig,
  sourceRegistry
} from "../content/pop-media";

function cleanJson(data: unknown) {
  return JSON.stringify(data).replace(/</g, "\\u003c");
}

export function buildGlobalJsonLd() {
  const organizationId = absoluteUrl("/#organization");
  const websiteId = absoluteUrl("/#website");

  return {
    "@context": "https://schema.org",
    "@graph": [
      {
        "@type": ["Organization", "LocalBusiness"],
        "@id": organizationId,
        name: siteConfig.name,
        legalName: siteConfig.legalName,
        alternateName: siteConfig.primaryBrand,
        description: siteConfig.description,
        url: absoluteUrl("/"),
        email: siteConfig.email,
        identifier: siteConfig.legalIdentifier,
        founder: {
          "@type": "Person",
          name: siteConfig.founder
        },
        address: {
          "@type": "PostalAddress",
          addressLocality: "Roeselare",
          addressCountry: "BE"
        },
        areaServed: {
          "@type": "Country",
          name: siteConfig.serviceArea
        },
        knowsAbout: siteConfig.keywords,
        sameAs: [
          siteConfig.radioUrl,
          siteConfig.publicitateUrl,
          siteConfig.legalMentionsUrl,
          ...siteConfig.socialProfiles
        ]
      },
      {
        "@type": "WebSite",
        "@id": websiteId,
        name: `${siteConfig.name} / ${siteConfig.primaryBrand}`,
        url: absoluteUrl("/"),
        inLanguage: siteConfig.language,
        publisher: {
          "@id": organizationId
        },
        about: {
          "@id": organizationId
        }
      },
      {
        "@type": "RadioStation",
        "@id": absoluteUrl("/#radio-belgia"),
        name: siteConfig.primaryBrand,
        url: siteConfig.radioUrl,
        areaServed: siteConfig.serviceArea,
        parentOrganization: {
          "@id": organizationId
        }
      },
      ...serviceEntities.map((service) => ({
        "@type": "Service",
        "@id": absoluteUrl(`${service.url}#service`),
        name: service.name,
        description: service.description,
        url: absoluteUrl(service.url),
        provider: {
          "@id": organizationId
        },
        areaServed: siteConfig.serviceArea
      })),
      ...businessProducts.map((product) => ({
        "@type": "Service",
        "@id": absoluteUrl(`${product.page}#${product.slug}`),
        name: product.name,
        alternateName: product.shortName,
        description: product.summary,
        url: absoluteUrl(product.page),
        provider: {
          "@id": organizationId
        },
        areaServed: siteConfig.serviceArea,
        audience: {
          "@type": "Audience",
          audienceType: product.audience
        },
        category: product.status,
        isRelatedTo: product.sourceUrls.map((url) => ({
          "@type": "WebPage",
          url
        }))
      })),
      ...entityPages.map((page) => ({
        "@type": "WebPage",
        "@id": absoluteUrl(`/${page.slug}#webpage`),
        name: page.title,
        description: page.description,
        url: absoluteUrl(`/${page.slug}`),
        inLanguage: siteConfig.language,
        isPartOf: {
          "@id": websiteId
        },
        about: {
          "@id": organizationId
        }
      })),
      {
        "@type": "ItemList",
        "@id": absoluteUrl("/surse#sources"),
        name: "Surse publice Pop Media / Radio Belgia",
        itemListElement: sourceRegistry.map((source, index) => ({
          "@type": "ListItem",
          position: index + 1,
          name: source.label,
          url: source.url
        }))
      },
      ...machineReadableFiles.map((file) => ({
        "@type": "Dataset",
        "@id": absoluteUrl(`${file.path}#dataset`),
        name: file.title,
        description: file.description,
        url: absoluteUrl(file.path),
        inLanguage: siteConfig.language,
        creator: {
          "@id": organizationId
        },
        publisher: {
          "@id": organizationId
        },
        about: {
          "@id": organizationId
        },
        isAccessibleForFree: true
      }
      ))
    ]
  };
}

export function JsonLdScript({ nonce }: { nonce?: string }) {
  return (
    <script
      nonce={nonce}
      suppressHydrationWarning
      type="application/ld+json"
      dangerouslySetInnerHTML={{ __html: cleanJson(buildGlobalJsonLd()) }}
    />
  );
}
