import type { Thing, WithContext } from "schema-dts";

interface JsonLdProps<T extends Thing> {
  data: WithContext<T>;
}

export function JsonLd<T extends Thing>({ data }: JsonLdProps<T>) {
  return (
    <script
      type="application/ld+json"
      dangerouslySetInnerHTML={{ __html: JSON.stringify(data) }}
    />
  );
}

const BASE_URL = "https://llmhosts.com";

export function OrganizationJsonLd() {
  return (
    <JsonLd
      data={{
        "@context": "https://schema.org",
        "@type": "Organization",
        "@id": `${BASE_URL}/#org`,
        name: "LLMHosts",
        url: BASE_URL,
        logo: {
          "@type": "ImageObject",
          url: `${BASE_URL}/og/logo.png`,
        },
        description:
          "Turn your home GPU into production AI infrastructure accessible from anywhere.",
        sameAs: [
          "https://github.com/LookNoHandsMom/LLMHosts.com",
          "https://pypi.org/project/llmhosts/",
        ],
      }}
    />
  );
}

export function WebSiteJsonLd() {
  return (
    <JsonLd
      data={{
        "@context": "https://schema.org",
        "@type": "WebSite",
        "@id": `${BASE_URL}/#website`,
        url: BASE_URL,
        name: "LLMHosts",
        publisher: { "@id": `${BASE_URL}/#org` },
      }}
    />
  );
}

export function SoftwareApplicationJsonLd() {
  return (
    <JsonLd
      data={{
        "@context": "https://schema.org",
        "@type": "SoftwareApplication",
        "@id": `${BASE_URL}/#app`,
        name: "LLMHosts",
        applicationCategory: "DeveloperApplication",
        operatingSystem: "Linux, macOS, Windows",
        description:
          "CLI tool + SaaS platform that turns a home GPU into production AI infrastructure. OpenAI + Anthropic compatible proxy with intelligent routing and semantic cache.",
        url: BASE_URL,
        downloadUrl: "https://pypi.org/project/llmhosts/",
        offers: [
          {
            "@type": "Offer",
            price: "0",
            priceCurrency: "USD",
            name: "Free",
            description: "Open source. Local inference. No account needed.",
          },
          {
            "@type": "Offer",
            price: "29",
            priceCurrency: "USD",
            name: "Pro",
            description: "Cloud intelligence. Multi-device mesh. 5 seats.",
          },
          {
            "@type": "Offer",
            price: "99",
            priceCurrency: "USD",
            name: "Team",
            description: "Hive Mode GPU pooling. RBAC. Shared configs.",
          },
        ],
        provider: { "@id": `${BASE_URL}/#org` },
      }}
    />
  );
}

interface BreadcrumbItem {
  name: string;
  url: string;
}

export function BreadcrumbJsonLd({ items }: { items: BreadcrumbItem[] }) {
  return (
    <JsonLd
      data={{
        "@context": "https://schema.org",
        "@type": "BreadcrumbList",
        itemListElement: items.map((item, index) => ({
          "@type": "ListItem",
          position: index + 1,
          name: item.name,
          item: item.url,
        })),
      }}
    />
  );
}

interface FAQItem {
  question: string;
  answer: string;
}

export function FAQPageJsonLd({ items }: { items: FAQItem[] }) {
  return (
    <JsonLd
      data={{
        "@context": "https://schema.org",
        "@type": "FAQPage",
        mainEntity: items.map((item) => ({
          "@type": "Question",
          name: item.question,
          acceptedAnswer: {
            "@type": "Answer",
            text: item.answer,
          },
        })),
      }}
    />
  );
}

interface WebPageJsonLdProps {
  name: string;
  description: string;
  url: string;
  dateModified?: string;
}

export function WebPageJsonLd({ name, description, url, dateModified }: WebPageJsonLdProps) {
  return (
    <JsonLd
      data={{
        "@context": "https://schema.org",
        "@type": "WebPage",
        "@id": `${url}#webpage`,
        name,
        description,
        url,
        isPartOf: { "@id": `${BASE_URL}/#website` },
        ...(dateModified && { dateModified }),
      }}
    />
  );
}
