import { contactAddress, contactEmail, contactPhoneDisplay, instagramUrl } from "@/lib/contact";

export const siteUrl = (process.env.NEXT_PUBLIC_SITE_URL ?? "https://www.ridenrevive.com").replace(
  /\/+$/,
  "",
);

export const siteName = "Ride N Revive";

type JsonLdValue =
  | string
  | number
  | boolean
  | null
  | undefined
  | JsonLdValue[]
  | { [key: string]: JsonLdValue };

export type JsonLdObject = {
  [key: string]: JsonLdValue;
};

export function absoluteSiteUrl(path = "/") {
  if (/^https?:\/\//i.test(path)) {
    return path;
  }

  const pathname = path.startsWith("/") ? path : `/${path}`;
  return `${siteUrl}${pathname}`;
}

export function JsonLd({ data }: { data: JsonLdObject | JsonLdObject[] }) {
  return (
    <script
      type="application/ld+json"
      dangerouslySetInnerHTML={{
        __html: JSON.stringify(data).replace(/</g, "\\u003c"),
      }}
    />
  );
}

export function organizationJsonLd(): JsonLdObject {
  return {
    "@context": "https://schema.org",
    "@type": ["Organization", "LocalBusiness", "AutoRepair"],
    "@id": `${siteUrl}/#organization`,
    name: siteName,
    url: `${siteUrl}/`,
    logo: absoluteSiteUrl("/images/new-logo.png"),
    image: absoluteSiteUrl("/images/new-logo.png"),
    email: contactEmail,
    telephone: contactPhoneDisplay,
    address: {
      "@type": "PostalAddress",
      streetAddress: contactAddress,
      addressCountry: "IN",
    },
    sameAs: [instagramUrl],
  };
}

export function websiteJsonLd(): JsonLdObject {
  return {
    "@context": "https://schema.org",
    "@type": "WebSite",
    "@id": `${siteUrl}/#website`,
    name: siteName,
    url: `${siteUrl}/`,
    publisher: {
      "@id": `${siteUrl}/#organization`,
    },
  };
}

export function serviceJsonLd({
  name,
  description,
  path,
  serviceType,
  areaServed = "India",
}: {
  name: string;
  description: string;
  path: string;
  serviceType?: string;
  areaServed?: string;
}): JsonLdObject {
  return {
    "@context": "https://schema.org",
    "@type": "Service",
    "@id": `${absoluteSiteUrl(path)}#service`,
    name,
    description,
    serviceType: serviceType ?? name,
    url: absoluteSiteUrl(path),
    provider: {
      "@id": `${siteUrl}/#organization`,
    },
    areaServed: {
      "@type": areaServed === "India" ? "Country" : "City",
      name: areaServed,
    },
  };
}

export function breadcrumbJsonLd(items: Array<{ name: string; path: string }>): JsonLdObject {
  return {
    "@context": "https://schema.org",
    "@type": "BreadcrumbList",
    itemListElement: items.map((item, index) => ({
      "@type": "ListItem",
      position: index + 1,
      name: item.name,
      item: absoluteSiteUrl(item.path),
    })),
  };
}

export function faqJsonLd(faqs: Array<{ question: string; answer: string }>): JsonLdObject | null {
  const mainEntity = faqs
    .filter((faq) => faq.question.trim() && faq.answer.trim())
    .map((faq) => ({
      "@type": "Question",
      name: faq.question,
      acceptedAnswer: {
        "@type": "Answer",
        text: faq.answer,
      },
    }));

  if (mainEntity.length === 0) {
    return null;
  }

  return {
    "@context": "https://schema.org",
    "@type": "FAQPage",
    mainEntity,
  };
}
