import Link from "next/link";
import { notFound } from "next/navigation";
import BlockResolver from "@/components/BlockResolver/BlockResolver";
import type { Metadata } from "next";
import { unescapeHtml } from "@/utils/unescapeHtml";
import PageTitle from "@/blocks/PageTitle";
import Section from "@/components/Section/Section";
import Container from "@/components/Container/Container";
import { generateMeta } from "@/utils/generateMeta";
import { stripHtmlTags } from "@/utils/stripHtmlTags";
import Badge from "@/components/Badge/Badge";
import { DEFAULT_OG_IMAGE } from "@/utils/constants";
import { Article, WithContext } from "schema-dts";
import { getCachedPSPost } from "@/services/prestashop-wp";

const Post = async (props: { params: Promise<{ postSlug: string }> }) => {
  const params = await props.params;
  const posts = await getCachedPSPost(params.postSlug, ["publish", "private", "draft"]);
  const post = posts[0];

  if (!post) {
    notFound();
  }

  const title = unescapeHtml(post.title.rendered);
  const description = stripHtmlTags(unescapeHtml(post.excerpt.rendered || ""));
  const siteUrl = process.env.SITE_URL || "https://teapot.agency";
  const url = `${siteUrl}/prestashop/insights-and-resources/${params.postSlug}`;
  const image = post.featured_media?.source_url || DEFAULT_OG_IMAGE;
  const datePublished = post.date;
  const dateModified = post.modified || post.date;
  const authorName = post.author?.name || "Teapot";

  const articleJsonLd: WithContext<Article> = {
    "@context": "https://schema.org",
    "@type": "Article",
    headline: title,
    description,
    url,
    mainEntityOfPage: url,
    image: [image],
    datePublished,
    dateModified,
    author: {
      "@type": "Person",
      name: authorName,
    },
    publisher: {
      "@type": "Organization",
      name: "Teapot",
      logo: {
        "@type": "ImageObject",
        url: `${siteUrl}/image/teapot.svg`,
      },
    },
  };

  return (
    <>
      <script
        type="application/ld+json"
        dangerouslySetInnerHTML={{ __html: JSON.stringify(articleJsonLd) }}
      />
      <PageTitle title={title} supporting_title={"INSIGHTS & RESOURCES"} />
      <BlockResolver blocks={post.blocks_json} />
      <Container>
        <Section className="grid auto-rows-auto grid-cols-1 p-none" gridArea="thin">
          <p className="h3 lg:px-none lg:m-none w-full">
            <Link
              href={"/insights-and-resources"}
              className={"flex flex-row justify-start items-center gap-xs"}
            >
              <Badge className={"mt-[3px] select-none transform rotate-[225deg]"}>
                <svg
                  xmlns="http://www.w3.org/2000/svg"
                  height="24"
                  viewBox="0 -960 960 960"
                  width="24"
                >
                  <path d="M647-440H160v-80h487L423-744l57-56 320 320-320 320-57-56 224-224Z" />
                </svg>
              </Badge>
              <span>Back to Insights & Resources</span>
            </Link>
          </p>
        </Section>
      </Container>
    </>
  );
};

export async function generateStaticParams() {
  return [];
}

export const generateMetadata = async (props: {
  params: Promise<{ postSlug: string }>;
}): Promise<Metadata> => {
  const params = await props.params;
  const post = await getCachedPSPost(params.postSlug, ["publish", "private", "draft"]);

  return generateMeta(post, "prestashop/insights-and-resources");
};

export default Post;
