import type { Metadata } from "next";
import {
  absoluteUrl,
  defaultDescription,
  defaultTitle,
  getSiteUrl,
  siteName,
} from "@/lib/site";

type MetadataInput = {
  canonicalPath?: string;
  description?: string;
  index?: boolean;
  title?: string;
};

export function buildMetadata({
  canonicalPath = "/",
  description = defaultDescription,
  index = true,
  title,
}: MetadataInput = {}): Metadata {
  const resolvedTitle = title
    ? `${title} | ${siteName}`
    : `${defaultTitle} | ${siteName}`;

  return {
    title: resolvedTitle,
    description,
    metadataBase: new URL(getSiteUrl()),
    alternates: {
      canonical: canonicalPath,
    },
    openGraph: {
      title: resolvedTitle,
      description,
      url: absoluteUrl(canonicalPath),
      siteName,
      locale: "bg_BG",
      type: "website",
    },
    twitter: {
      card: "summary_large_image",
      title: resolvedTitle,
      description,
    },
    robots: index
      ? {
          index: true,
          follow: true,
        }
      : {
          index: false,
          follow: false,
          googleBot: {
            index: false,
            follow: false,
            noimageindex: true,
          },
        },
  };
}
