import { URUNLER } from "./credits";

/**
 * Kanonik site adresi. NEXT_PUBLIC_APP_URL bilinçli olarak KULLANILMAZ —
 * o değişken ödeme callback akışına ait ve .env.local'de localhost'a işaret
 * ediyor. .dockerignore .env* dosyalarını dışladığı için prod build burada
 * deterministik olarak kolaytercih.com'a düşer.
 */
export const SITE_URL =
  process.env.NEXT_PUBLIC_SITE_URL ??
  (process.env.NODE_ENV === "production"
    ? "https://kolaytercih.com"
    : "http://localhost:3000");

export const SITE_NAME = "KolayTercih";

type JsonLdData = Record<string, unknown>;

export function organizationJsonLd(): JsonLdData {
  return {
    "@context": "https://schema.org",
    "@type": "Organization",
    name: SITE_NAME,
    url: SITE_URL,
    logo: `${SITE_URL}/kolay-tercih-mark.svg`,
    contactPoint: {
      "@type": "ContactPoint",
      email: "destek@kolaytercih.com",
      contactType: "customer support",
      availableLanguage: "Turkish",
    },
  };
}

export function webSiteJsonLd(): JsonLdData {
  return {
    "@context": "https://schema.org",
    "@type": "WebSite",
    name: SITE_NAME,
    url: SITE_URL,
    inLanguage: "tr",
  };
}

export function softwareApplicationJsonLd(): JsonLdData {
  return {
    "@context": "https://schema.org",
    "@type": "SoftwareApplication",
    name: SITE_NAME,
    applicationCategory: "EducationalApplication",
    operatingSystem: "Web",
    url: SITE_URL,
    inLanguage: "tr",
    offers: {
      "@type": "Offer",
      price: (URUNLER.paket.amountKurus / 100).toFixed(0),
      priceCurrency: "TRY",
      description: URUNLER.paket.label,
    },
  };
}

export function faqPageJsonLd(
  faqs: readonly { question: string; answer: string }[],
): JsonLdData {
  return {
    "@context": "https://schema.org",
    "@type": "FAQPage",
    mainEntity: faqs.map((f) => ({
      "@type": "Question",
      name: f.question,
      acceptedAnswer: { "@type": "Answer", text: f.answer },
    })),
  };
}

export function breadcrumbJsonLd(
  items: readonly { name: string; path: string }[],
): JsonLdData {
  return {
    "@context": "https://schema.org",
    "@type": "BreadcrumbList",
    itemListElement: items.map((item, i) => ({
      "@type": "ListItem",
      position: i + 1,
      name: item.name,
      item: `${SITE_URL}${item.path}`,
    })),
  };
}

export function articleJsonLd(opts: {
  title: string;
  description: string;
  path: string;
  datePublished: string;
}): JsonLdData {
  return {
    "@context": "https://schema.org",
    "@type": "Article",
    headline: opts.title,
    description: opts.description,
    url: `${SITE_URL}${opts.path}`,
    datePublished: opts.datePublished,
    inLanguage: "tr",
    publisher: {
      "@type": "Organization",
      name: SITE_NAME,
      url: SITE_URL,
    },
  };
}

/** JSON-LD'yi güvenli gömer: `<` kaçışı script-injection'ı engeller. */
export function JsonLd({ data }: { data: JsonLdData }) {
  return (
    <script
      type="application/ld+json"
      dangerouslySetInnerHTML={{
        __html: JSON.stringify(data).replace(/</g, "\\u003c"),
      }}
    />
  );
}
