// Central SEO constants and JSON-LD structured-data builders.
// Used across the marketing site so titles, canonicals, Open Graph,
// and schema.org markup all stay consistent.

/** Canonical production origin. Falls back to the known prod URL. */
export const SITE_URL = (
  process.env.NEXT_PUBLIC_APP_URL || "https://tia.manohargupta.com"
).replace(/\/$/, "");

export const SITE_NAME = "Tia";
export const SITE_TAGLINE = "Your baby's digital heirloom";
export const SITE_DESCRIPTION =
  "Tia is a digital heirloom for your baby — not just a tracker. Log every feed, milestone, and memory, track the IAP vaccination schedule with Telegram alerts, and build a living, private archive your child will one day inherit. Built for Indian families.";

/** Absolute URL helper — keeps canonical/OG urls consistent. */
export function absoluteUrl(path = "/"): string {
  if (path.startsWith("http")) return path;
  return `${SITE_URL}${path.startsWith("/") ? path : `/${path}`}`;
}

// ── JSON-LD builders ─────────────────────────────────────────────

/** Organization — publisher identity, reused as the publisher of articles. */
export function organizationSchema() {
  return {
    "@context": "https://schema.org",
    "@type": "Organization",
    "@id": `${SITE_URL}/#organization`,
    name: SITE_NAME,
    url: SITE_URL,
    logo: {
      "@type": "ImageObject",
      url: absoluteUrl("/icons/512.png"),
      width: 512,
      height: 512,
    },
    description: SITE_DESCRIPTION,
    foundingDate: "2026",
    areaServed: "IN",
    email: "hello@tia.baby",
    contactPoint: {
      "@type": "ContactPoint",
      telephone: "+91-95548-81799",
      contactType: "customer support",
      email: "hello@tia.baby",
      areaServed: "IN",
      availableLanguage: ["English", "Hindi"],
    },
  };
}

/** WebSite — enables sitelinks/search treatment. */
export function websiteSchema() {
  return {
    "@context": "https://schema.org",
    "@type": "WebSite",
    "@id": `${SITE_URL}/#website`,
    name: SITE_NAME,
    url: SITE_URL,
    description: SITE_DESCRIPTION,
    inLanguage: "en-IN",
    publisher: { "@id": `${SITE_URL}/#organization` },
  };
}

/** SoftwareApplication — tells search engines this is an app, free to use. */
export function softwareApplicationSchema() {
  return {
    "@context": "https://schema.org",
    "@type": "SoftwareApplication",
    name: `${SITE_NAME} — Baby Tracker & Digital Heirloom`,
    operatingSystem: "Web, iOS, Android",
    applicationCategory: "HealthApplication",
    description: SITE_DESCRIPTION,
    url: SITE_URL,
    image: absoluteUrl("/icons/512.png"),
    inLanguage: "en-IN",
    offers: {
      "@type": "Offer",
      price: "0",
      priceCurrency: "INR",
      availability: "https://schema.org/InStock",
    },
    publisher: { "@id": `${SITE_URL}/#organization` },
  };
}

/** BreadcrumbList from an ordered list of { name, path }. */
export function breadcrumbSchema(items: { name: string; path: string }[]) {
  return {
    "@context": "https://schema.org",
    "@type": "BreadcrumbList",
    itemListElement: items.map((item, i) => ({
      "@type": "ListItem",
      position: i + 1,
      name: item.name,
      item: absoluteUrl(item.path),
    })),
  };
}

/** BlogPosting / Article schema for a single post. */
export function blogPostingSchema(post: {
  slug: string;
  title: string;
  excerpt: string;
  date: string;
  author: string;
}) {
  const url = absoluteUrl(`/blog/${post.slug}`);
  return {
    "@context": "https://schema.org",
    "@type": "BlogPosting",
    "@id": `${url}#article`,
    headline: post.title,
    description: post.excerpt,
    datePublished: post.date,
    dateModified: post.date,
    inLanguage: "en-IN",
    image: absoluteUrl(`/blog/${post.slug}/opengraph-image`),
    author: { "@type": "Organization", name: post.author, url: SITE_URL },
    publisher: { "@id": `${SITE_URL}/#organization` },
    mainEntityOfPage: { "@type": "WebPage", "@id": url },
  };
}

/**
 * Builds the `dangerouslySetInnerHTML` payload for a JSON-LD <script>.
 * Pass one schema object or an array. Set `type="application/ld+json"` on
 * the <script> element itself.
 */
export function jsonLdScript(data: object | object[]): { __html: string } {
  // JSON.stringify is XSS-safe here because all inputs are our own constants.
  return { __html: JSON.stringify(data) };
}
