import type { Metadata } from "next";
import { siteConfig } from "@/data/site";
import { IS_STAGING } from "@/lib/environment";

const defaultImage = "/images/heroes/homepage-hero.webp";

export const seoKeywords = [
  "Marloth Park accommodation",
  "Marloth Park holiday homes",
  "Marloth Park self-catering",
  "Kruger accommodation",
  "Kruger National Park accommodation",
  "accommodation near Kruger National Park",
  "accommodation near Crocodile Bridge Gate",
  "Marloth Park safari drives",
  "Kruger safari drives",
  "things to do in Marloth Park",
  "Marloth Park restaurants",
  "Marloth Park businesses",
  "Marloth Park services",
  "Marloth Park directory",
];

export function absoluteUrl(path = "/") {
  const cleanPath = path.startsWith("/") ? path : `/${path}`;
  return new URL(cleanPath, siteConfig.url).toString();
}

export function createPageMetadata({
  title,
  description,
  path,
  image = defaultImage,
  keywords = [],
  noIndex = false,
}: {
  title: string;
  description: string;
  path: string;
  image?: string;
  keywords?: string[];
  noIndex?: boolean;
}): Metadata {
  const canonical = absoluteUrl(path);
  const imageUrl = absoluteUrl(image);
  const effectiveNoIndex = noIndex || IS_STAGING;

  return {
    title,
    description,
    keywords: [...seoKeywords, ...keywords],
    metadataBase: new URL(siteConfig.url),
    alternates: { canonical },
    robots: effectiveNoIndex
      ? { index: false, follow: false, nocache: true }
      : {
          index: true,
          follow: true,
          googleBot: {
            index: true,
            follow: true,
            "max-image-preview": "large",
            "max-snippet": -1,
            "max-video-preview": -1,
          },
        },
    openGraph: {
      title,
      description,
      url: canonical,
      siteName: siteConfig.name,
      locale: "en_ZA",
      type: "website",
      images: [
        {
          url: imageUrl,
          width: 1200,
          height: 630,
          alt: `${siteConfig.name} - Marloth Park local listings near Kruger`,
        },
      ],
    },
    twitter: {
      card: "summary_large_image",
      title,
      description,
      images: [imageUrl],
    },
  };
}

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

export function websiteSchema() {
  return {
    "@context": "https://schema.org",
    "@type": "WebSite",
    name: siteConfig.name,
    url: siteConfig.url,
    description:
      "Marloth Park Central helps visitors find Marloth Park accommodation, safari drives, restaurants, activities, local businesses and services near Kruger National Park.",
    inLanguage: "en-ZA",
  };
}

export function tourismOrganizationSchema() {
  return {
    "@context": "https://schema.org",
    "@type": ["Organization", "TouristInformationCenter"],
    name: siteConfig.name,
    url: siteConfig.url,
    email: siteConfig.partnerEmail,
    description:
      "Marloth Park local discovery platform for accommodation, safari drives, restaurants, activities, businesses and services near Kruger National Park.",
    areaServed: [
      { "@type": "Place", name: "Marloth Park, Mpumalanga, South Africa" },
      { "@type": "Place", name: "Kruger National Park southern gate area" },
      { "@type": "Place", name: "Crocodile Bridge Gate" },
      { "@type": "Place", name: "Komatipoort" },
    ],
    address: {
      "@type": "PostalAddress",
      addressLocality: "Marloth Park",
      addressRegion: "Mpumalanga",
      addressCountry: "ZA",
    },
    sameAs: [],
  };
}

export function collectionPageSchema({
  name,
  description,
  path,
  itemNames = [],
}: {
  name: string;
  description: string;
  path: string;
  itemNames?: string[];
}) {
  return {
    "@context": "https://schema.org",
    "@type": "CollectionPage",
    name,
    description,
    url: absoluteUrl(path),
    isPartOf: { "@type": "WebSite", name: siteConfig.name, url: siteConfig.url },
    about: itemNames.map((name) => ({ "@type": "Thing", name })),
  };
}

export function productSchema({
  name,
  description,
  path,
  image,
  price,
  category,
}: {
  name: string;
  description: string;
  path: string;
  image?: string;
  price?: number;
  category?: string;
}) {
  return {
    "@context": "https://schema.org",
    "@type": "Product",
    name,
    description,
    image: image ? absoluteUrl(image) : undefined,
    url: absoluteUrl(path),
    category,
    brand: { "@type": "Brand", name: siteConfig.name },
    offers: price
      ? {
          "@type": "Offer",
          price,
          priceCurrency: "ZAR",
          availability: "https://schema.org/InStock",
          url: absoluteUrl(path),
          hasMerchantReturnPolicy: {
            "@type": "MerchantReturnPolicy",
            applicableCountry: "ZA",
            returnPolicyCategory: "https://schema.org/MerchantReturnNotPermitted",
            merchantReturnLink: absoluteUrl("/delivery-refunds"),
          },
          shippingDetails: {
            "@type": "OfferShippingDetails",
            shippingDestination: { "@type": "DefinedRegion", addressCountry: "ZA" },
            shippingRate: { "@type": "MonetaryAmount", value: 0, currency: "ZAR" },
            deliveryTime: {
              "@type": "ShippingDeliveryTime",
              handlingTime: { "@type": "QuantitativeValue", minValue: 0, maxValue: 2, unitCode: "DAY" },
              transitTime: { "@type": "QuantitativeValue", minValue: 0, maxValue: 0, unitCode: "DAY" },
            },
          },
        }
      : undefined,
  };
}

export function lodgingSchema({
  name,
  description,
  path,
  image,
  priceRange,
}: {
  name: string;
  description: string;
  path: string;
  image?: string;
  priceRange?: string;
}) {
  return {
    "@context": "https://schema.org",
    "@type": "LodgingBusiness",
    name,
    description,
    url: absoluteUrl(path),
    image: image ? absoluteUrl(image) : undefined,
    priceRange,
    address: {
      "@type": "PostalAddress",
      addressLocality: "Marloth Park",
      addressRegion: "Mpumalanga",
      addressCountry: "ZA",
    },
    areaServed: [
      { "@type": "Place", name: "Marloth Park" },
      { "@type": "Place", name: "Kruger National Park" },
      { "@type": "Place", name: "Crocodile Bridge Gate" },
    ],
  };
}
