// Centralized SEO helpers and constants.
export const SITE_URL = "https://biblewordoftheday.com";
export const SITE_NAME = "Bible Word of the Day";
export const DEFAULT_OG_IMAGE = `${SITE_URL}/og-image.jpg`;
export const SITE_LOGO = `${SITE_URL}/logo.png`;

export function absUrl(path: string): string {
  if (!path) return SITE_URL;
  if (path.startsWith("http")) return path;
  const p = path.startsWith("/") ? path : `/${path}`;
  return `${SITE_URL}${p}`.replace(/\/$/, p === "/" ? "/" : "");
}

type MetaTag = Record<string, string>;
type LinkTag = Record<string, string>;
type ScriptTag = { type: string; children: string };

export interface PageSeoInput {
  title: string;
  description: string;
  path: string;
  image?: string;
  type?: "website" | "article";
  noindex?: boolean;
}

export function pageSeo({
  title,
  description,
  path,
  image = DEFAULT_OG_IMAGE,
  type = "website",
  noindex = false,
}: PageSeoInput): { meta: MetaTag[]; links: LinkTag[] } {
  const url = absUrl(path);
  return {
    meta: [
      { title },
      { name: "description", content: description },
      { name: "robots", content: noindex ? "noindex,follow" : "index,follow" },
      { property: "og:title", content: title },
      { property: "og:description", content: description },
      { property: "og:url", content: url },
      { property: "og:type", content: type },
      { property: "og:site_name", content: SITE_NAME },
      { property: "og:image", content: image },
      { name: "twitter:card", content: "summary_large_image" },
      { name: "twitter:title", content: title },
      { name: "twitter:description", content: description },
      { name: "twitter:image", content: image },
    ],
    links: noindex ? [] : [{ rel: "canonical", href: url }],
  };
}

export function jsonLd(data: unknown): ScriptTag {
  return { type: "application/ld+json", children: JSON.stringify(data) };
}

export function breadcrumb(items: { name: string; path: string }[]): ScriptTag {
  return jsonLd({
    "@context": "https://schema.org",
    "@type": "BreadcrumbList",
    itemListElement: items.map((it, i) => ({
      "@type": "ListItem",
      position: i + 1,
      name: it.name,
      item: absUrl(it.path),
    })),
  });
}
