import type { Blog } from "@/services/blogs";

type BlogDetailSchemaProps = {
  blog: Blog;
};

export default function BlogDetailSchema({
  blog,
}: BlogDetailSchemaProps) {
  const blogUrl =
    blog.canonicalUrl ||
    `https://www.thegrandheritageresorts.com/blogs/${blog.slug}`;

  const blogImage =
    blog.coverImage ||
    blog.ogImage;

  const description =
    blog.metaDescription ||
    blog.excerpt ||
    blog.title;

  const schema = {
    "@context": "https://schema.org",
    "@graph": [
      {
        "@type": "BlogPosting",
        "@id": `${blogUrl}#article`,
        url: blogUrl,

        headline: blog.title,

        name: blog.title,

        description,

        image: blogImage
          ? [blogImage]
          : undefined,

        author: {
          "@type": "Person",
          name:
            blog.author ||
            "The Grand Heritage Resorts",
        },

        publisher: {
          "@type": "Resort",
          "@id":
            "https://www.thegrandheritageresorts.com/#resort",
          name: "The Grand Heritage Resorts",
          url: "https://www.thegrandheritageresorts.com/",
          logo: {
            "@type": "ImageObject",
            url: "https://pub-b0bc1a7c71a54576bbf43595e30e6188.r2.dev/images/hero/the-grand-heritage-resorts-logo.png",
          },
        },

        mainEntityOfPage: {
          "@type": "WebPage",
          "@id": blogUrl,
        },

        articleSection: blog.category,

        keywords: blog.metaKeywords?.join(", "),

        datePublished: blog.publishedAt
          ? new Date(
              blog.publishedAt
            ).toISOString()
          : undefined,

        dateModified: blog.publishedAt
          ? new Date(
              blog.publishedAt
            ).toISOString()
          : undefined,
      },

      {
        "@type": "WebPage",
        "@id": `${blogUrl}#webpage`,
        url: blogUrl,

        name: blog.title,

        description,

        isPartOf: {
          "@id":
            "https://www.thegrandheritageresorts.com/#website",
        },

        about: {
          "@id":
            "https://www.thegrandheritageresorts.com/#resort",
        },

        mainEntity: {
          "@id": `${blogUrl}#article`,
        },

        breadcrumb: {
          "@id": `${blogUrl}#breadcrumb`,
        },
      },

      {
        "@type": "Resort",
        "@id":
          "https://www.thegrandheritageresorts.com/#resort",

        name: "The Grand Heritage Resorts",

        url: "https://www.thegrandheritageresorts.com/",

        logo: {
          "@type": "ImageObject",
          url: "https://pub-b0bc1a7c71a54576bbf43595e30e6188.r2.dev/images/hero/the-grand-heritage-resorts-logo.png",
        },

        telephone: [
          "+91 74472 74472",
          "+91 74477 84778",
        ],

        email:
          "booking@thegrandheritageresorts.com",

        address: {
          "@type": "PostalAddress",
          addressLocality:
            "Kasari, Shikrapur",
          addressRegion: "Maharashtra",
          addressCountry: "IN",
        },

        geo: {
          "@type": "GeoCoordinates",
          latitude: 18.708709,
          longitude: 74.179304,
        },
      },

      {
        "@type": "BreadcrumbList",
        "@id": `${blogUrl}#breadcrumb`,

        itemListElement: [
          {
            "@type": "ListItem",
            position: 1,
            name: "Home",
            item:
              "https://www.thegrandheritageresorts.com/",
          },
          {
            "@type": "ListItem",
            position: 2,
            name: "Blogs",
            item:
              "https://www.thegrandheritageresorts.com/blogs",
          },
          {
            "@type": "ListItem",
            position: 3,
            name: blog.title,
            item: blogUrl,
          },
        ],
      },
    ],
  };

  return (
    <script
      type="application/ld+json"
      dangerouslySetInnerHTML={{
        __html: JSON.stringify(schema),
      }}
    />
  );
}