import { notFound } from "next/navigation"
import { loadImageMapping } from "@/lib/wordpress-content"
import { generateSEOMetadata, generateStructuredData } from "@/lib/seo"
import { getPageBySlug } from "@/lib/wordpress-data-loader"
import { cleanWordPressContent } from "@/lib/clean-wordPress-content"
import { DropdownPageShell } from "@/components/dropdown-page-shell"
import type { Metadata } from "next"

const WORDPRESS_SLUG = "reviews"

export async function generateMetadata(): Promise<Metadata> {
  const page = getPageBySlug(WORDPRESS_SLUG)

  if (!page) {
    return {
      title: "Page Not Found | Rocky Mountain Vending",
    }
  }

  return generateSEOMetadata({
    title: page.title || "Reviews",
    description: page.seoDescription || page.excerpt || "",
    excerpt: page.excerpt,
    date: page.date,
    modified: page.modified,
    image: page.images?.[0]?.localPath,
    path: "/blog/reviews",
  })
}

export default async function ReviewsPage() {
  try {
    const page = getPageBySlug(WORDPRESS_SLUG)

    if (!page) {
      notFound()
    }

    let imageMapping: any = {}
    try {
      imageMapping = loadImageMapping()
    } catch (e) {
      imageMapping = {}
    }

    const content = page.content ? (
      <div className="max-w-none">
        {cleanWordPressContent(String(page.content), {
          imageMapping,
          pageTitle: page.title,
        })}
      </div>
    ) : (
      <p className="text-muted-foreground">No content available.</p>
    )

    let structuredData
    try {
      structuredData = generateStructuredData({
        title: page.title || "Reviews",
        description: page.seoDescription || page.excerpt || "",
        url:
          page.link ||
          page.urlPath ||
          `https://rockymountainvending.com/reviews/`,
        datePublished: page.date,
        dateModified: page.modified || page.date,
        type: "WebPage",
      })
    } catch (e) {
      structuredData = {
        "@context": "https://schema.org",
        "@type": "WebPage",
        headline: page.title || "Reviews",
        description: page.seoDescription || "",
        url: `https://rockymountainvending.com/reviews/`,
      }
    }

    return (
      <>
        <script
          type="application/ld+json"
          dangerouslySetInnerHTML={{ __html: JSON.stringify(structuredData) }}
        />
        <DropdownPageShell
          breadcrumbs={[
            { label: "Blog", href: "/blog" },
            { label: page.title || "Reviews", href: "/blog/reviews" },
          ]}
          eyebrow="Blog Posts"
          title={page.title || "Reviews"}
          description={
            page.seoDescription ||
            page.excerpt ||
            "Read Rocky Mountain Vending reviews, testimonials, and customer feedback gathered from businesses across Utah."
          }
          content={content}
          contentClassName="prose prose-lg max-w-none prose-headings:text-foreground prose-p:text-muted-foreground prose-a:text-foreground prose-a:underline prose-a:decoration-primary/35 prose-a:underline-offset-4 hover:prose-a:decoration-primary prose-strong:text-foreground"
          cta={{
            eyebrow: "Live Review Feed",
            title: "Want the current public review stream instead?",
            description:
              "For the live Google review feed and the latest customer feedback, head to the main reviews page.",
            actions: [
              { label: "View Live Reviews", href: "/reviews" },
              {
                label: "Talk to Our Team",
                href: "/contact-us#contact-form",
                variant: "outline",
              },
            ],
          }}
        />
      </>
    )
  } catch (error) {
    if (process.env.NODE_ENV === "development") {
      console.error("Error rendering Reviews page:", error)
    }
    notFound()
  }
}
