"use client";

import { siteConfig } from "@/config/site.config";

type OrganizationJsonLdProps = {
  locale?: string;
};

export function OrganizationJsonLd({ locale = "ar" }: OrganizationJsonLdProps) {
  const jsonLd = {
    "@context": "https://schema.org",
    "@type": "Organization",
    name: siteConfig.siteName,
    url: siteConfig.url,
    logo: `${siteConfig.url}/logo.png`,
    description:
      siteConfig.description[locale as keyof typeof siteConfig.description],
    address: {
      "@type": "PostalAddress",
      addressCountry: "SA",
      addressRegion: "Riyadh",
    },
    contactPoint: {
      "@type": "ContactPoint",
      contactType: "customer service",
      availableLanguage: ["Arabic", "English"],
    },
    sameAs: [
      "https://twitter.com/ataya_sa",
      "https://www.instagram.com/ataya_sa",
    ],
  };

  return (
    <script
      type="application/ld+json"
      dangerouslySetInnerHTML={{ __html: JSON.stringify(jsonLd) }}
    />
  );
}

type WebsiteJsonLdProps = {
  locale?: string;
};

export function WebsiteJsonLd({ locale = "ar" }: WebsiteJsonLdProps) {
  const jsonLd = {
    "@context": "https://schema.org",
    "@type": "WebSite",
    name: siteConfig.siteName,
    url: siteConfig.url,
    description:
      siteConfig.description[locale as keyof typeof siteConfig.description],
    inLanguage: locale === "ar" ? "en-EN" : "en-US",
    potentialAction: {
      "@type": "SearchAction",
      target: {
        "@type": "EntryPoint",
        urlTemplate: `${siteConfig.url}/${locale}/horses?search={search_term_string}`,
      },
      "query-input": "required name=search_term_string",
    },
  };

  return (
    <script
      type="application/ld+json"
      dangerouslySetInnerHTML={{ __html: JSON.stringify(jsonLd) }}
    />
  );
}

type BreadcrumbJsonLdProps = {
  items: Array<{
    name: string;
    url: string;
  }>;
};

export function BreadcrumbJsonLd({ items }: BreadcrumbJsonLdProps) {
  const jsonLd = {
    "@context": "https://schema.org",
    "@type": "BreadcrumbList",
    itemListElement: items.map((item, index) => ({
      "@type": "ListItem",
      position: index + 1,
      name: item.name,
      item: item.url,
    })),
  };

  return (
    <script
      type="application/ld+json"
      dangerouslySetInnerHTML={{ __html: JSON.stringify(jsonLd) }}
    />
  );
}

type ProductJsonLdProps = {
  name: string;
  description: string;
  image: string;
  price?: number;
  currency?: string;
  availability?: "InStock" | "OutOfStock" | "PreOrder";
  category?: string;
  brand?: string;
  url: string;
};

export function ProductJsonLd({
  name,
  description,
  image,
  price,
  currency = "SAR",
  availability = "InStock",
  category,
  brand = "Ataya",
  url,
}: ProductJsonLdProps) {
  const jsonLd: Record<string, unknown> = {
    "@context": "https://schema.org",
    "@type": "Product",
    name,
    description,
    image,
    url,
    brand: {
      "@type": "Brand",
      name: brand,
    },
  };

  if (category) {
    jsonLd.category = category;
  }

  if (price !== undefined && price > 0) {
    jsonLd.offers = {
      "@type": "Offer",
      price,
      priceCurrency: currency,
      availability: `https://schema.org/${availability}`,
      url,
    };
  }

  return (
    <script
      type="application/ld+json"
      dangerouslySetInnerHTML={{ __html: JSON.stringify(jsonLd) }}
    />
  );
}

type AuctionJsonLdProps = {
  name: string;
  description: string;
  image: string;
  startDate?: string;
  endDate?: string;
  location?: string;
  url: string;
  status?: "upcoming" | "running" | "finished";
};

export function AuctionJsonLd({
  name,
  description,
  image,
  startDate,
  endDate,
  location = "Saudi Arabia",
  url,
  status = "upcoming",
}: AuctionJsonLdProps) {
  const eventStatus =
    status === "finished"
      ? "https://schema.org/EventCancelled"
      : status === "running"
        ? "https://schema.org/EventScheduled"
        : "https://schema.org/EventScheduled";

  const jsonLd: Record<string, unknown> = {
    "@context": "https://schema.org",
    "@type": "Event",
    name,
    description,
    image,
    url,
    eventStatus,
    eventAttendanceMode: "https://schema.org/OnlineEventAttendanceMode",
    location: {
      "@type": "Place",
      name: location,
      address: {
        "@type": "PostalAddress",
        addressCountry: "SA",
      },
    },
    organizer: {
      "@type": "Organization",
      name: siteConfig.siteName,
      url: siteConfig.url,
    },
  };

  if (startDate) {
    jsonLd.startDate = startDate;
  }
  if (endDate) {
    jsonLd.endDate = endDate;
  }

  return (
    <script
      type="application/ld+json"
      dangerouslySetInnerHTML={{ __html: JSON.stringify(jsonLd) }}
    />
  );
}

type FAQJsonLdProps = {
  questions: Array<{
    question: string;
    answer: string;
  }>;
};

export function FAQJsonLd({ questions }: FAQJsonLdProps) {
  const jsonLd = {
    "@context": "https://schema.org",
    "@type": "FAQPage",
    mainEntity: questions.map((q) => ({
      "@type": "Question",
      name: q.question,
      acceptedAnswer: {
        "@type": "Answer",
        text: q.answer,
      },
    })),
  };

  return (
    <script
      type="application/ld+json"
      dangerouslySetInnerHTML={{ __html: JSON.stringify(jsonLd) }}
    />
  );
}
