import { SITE } from "@/lib/config";

type SchemaType =
  | "Organization"
  | "SoftwareApplication"
  | "FAQPage"
  | "Article"
  | "LocalBusiness";

interface JsonLdProps {
  type?: SchemaType;
  data?: Record<string, any>;
  override?: boolean; // If true, uses data exactly as passed without default templates
}

export default function JsonLd({
  type = "Organization",
  data = {},
  override = false,
}: JsonLdProps) {
  if (override && data) {
    return (
      <script
        type="application/ld+json"
        dangerouslySetInnerHTML={{ __html: JSON.stringify(data) }}
      />
    );
  }

  const baseUrl = `https://${SITE.domain}`;

  let schema: Record<string, any> = {
    "@context": "https://schema.org",
  };

  if (type === "Organization") {
    schema = {
      ...schema,
      "@type": "Organization",
      name: SITE.brand,
      url: baseUrl,
      logo: `${baseUrl}/images/logos/mawidi-logo.png`,
      description: "GCC AI receptionist for clinics & aesthetics.",
      sameAs: [
        // Add actual social links here when available
        "https://twitter.com/mawidi",
        "https://linkedin.com/company/mawidi",
      ],
      contactPoint: {
        "@type": "ContactPoint",
        contactType: "customer service",
        areaServed: ["SA", "AE", "QA", "KW", "BH", "OM"],
        availableLanguage: ["en", "ar"],
      },
      ...data,
    };
  } else if (type === "SoftwareApplication") {
    schema = {
      ...schema,
      "@type": "SoftwareApplication",
      name: "Mawidi AI Receptionist",
      applicationCategory: "BusinessApplication",
      operatingSystem: "Web, WhatsApp",
      offers: {
        "@type": "Offer",
        price: "149",
        priceCurrency: "USD",
        availability: "https://schema.org/InStock",
      },
      description:
        "Automated WhatsApp booking and patient management system for clinics.",
      featureList:
        "WhatsApp Automation, Appointment Booking, No-show Reduction, Multi-language Support",
      ...data,
    };
  } else if (type === "FAQPage") {
    schema = {
      ...schema,
      "@type": "FAQPage",
      mainEntity: data.mainEntity || [],
    };
  } else if (type === "LocalBusiness") {
    schema = {
      ...schema,
      "@type": ["LocalBusiness", "SoftwareApplication"],
      name: SITE.brand,
      description: "GCC AI receptionist for clinics & aesthetics.",
      url: baseUrl,
      logo: `${baseUrl}/images/logos/mawidi-logo.png`,
      address: {
        "@type": "PostalAddress",
        addressCountry: "SA",
        addressRegion: "Riyadh",
        addressLocality: "Riyadh",
      },
      geo: {
        "@type": "GeoCoordinates",
        latitude: "24.7136",
        longitude: "46.6753",
      },
      areaServed: [
        { "@type": "Country", name: "Saudi Arabia" },
        { "@type": "Country", name: "United Arab Emirates" },
        { "@type": "Country", name: "Qatar" },
        { "@type": "Country", name: "Kuwait" },
        { "@type": "Country", name: "Bahrain" },
        { "@type": "Country", name: "Oman" },
      ],
      availableLanguage: ["en", "ar"],
      priceRange: "$$",
      ...data,
    };
  }

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