import type { Metadata } from "next";
import { site } from "@/data/home";

export const SITE_URL =
  process.env.NEXT_PUBLIC_SITE_URL ?? "https://www.havaran.com";

export const SITE_NAME = site.name;
export const SITE_NAME_EN = site.nameEn;

export const DEFAULT_DESCRIPTION = site.description;

export const SITE_KEYWORDS = [
  "هواران",
  "رسام تجارت هواران",
  "HAVARAN",
  "فن تخلیه دود",
  "F300",
  "چیلر",
  "فن کویل",
  "هواساز",
  "تهویه مطبوع",
  "مدیریت دود",
  "جت فن",
  "تاسیسات ساختمانی",
];

export const DEFAULT_OG_IMAGE = "/images/brand/logo.png";

export function absoluteUrl(path = "/"): string {
  const normalized = path.startsWith("/") ? path : `/${path}`;
  return `${SITE_URL}${normalized === "/" ? "" : normalized}`;
}

type PageMetaInput = {
  title: string;
  description: string;
  path: string;
  image?: string;
  noIndex?: boolean;
};

export function pageMetadata({
  title,
  description,
  path,
  image = DEFAULT_OG_IMAGE,
  noIndex = false,
}: PageMetaInput): Metadata {
  const url = absoluteUrl(path);

  return {
    title,
    description,
    alternates: {
      canonical: path,
    },
    openGraph: {
      type: "website",
      locale: "fa_IR",
      url,
      siteName: SITE_NAME,
      title: `${title} | ${SITE_NAME}`,
      description,
      images: [
        {
          url: image,
          alt: title,
        },
      ],
    },
    twitter: {
      card: "summary_large_image",
      title: `${title} | ${SITE_NAME}`,
      description,
      images: [image],
    },
    robots: noIndex
      ? { index: false, follow: false }
      : { index: true, follow: true },
  };
}

export function organizationJsonLd() {
  return {
    "@context": "https://schema.org",
    "@type": "Organization",
    name: SITE_NAME,
    alternateName: SITE_NAME_EN,
    url: SITE_URL,
    logo: absoluteUrl("/images/brand/logo.png"),
    email: site.email,
    telephone: site.phones.map((phone) => phone.replace(/\s/g, "")),
    address: {
      "@type": "PostalAddress",
      addressCountry: "IR",
      addressLocality: "تهران",
      streetAddress: site.office,
    },
    contactPoint: [
      {
        "@type": "ContactPoint",
        telephone: "+982146144267",
        contactType: "customer service",
        areaServed: "IR",
        availableLanguage: ["fa", "en"],
      },
    ],
  };
}

export function websiteJsonLd() {
  return {
    "@context": "https://schema.org",
    "@type": "WebSite",
    name: SITE_NAME,
    alternateName: SITE_NAME_EN,
    url: SITE_URL,
    inLanguage: "fa-IR",
    publisher: {
      "@type": "Organization",
      name: SITE_NAME,
      logo: {
        "@type": "ImageObject",
        url: absoluteUrl("/images/brand/logo.png"),
      },
    },
  };
}
