import { notFound } from "next/navigation"
import { loadImageMapping } from "@/lib/wordpress-content"
import { buildAbsoluteUrl } from "@/lib/seo-registry"
import { generateSEOMetadata, generateStructuredData } from "@/lib/seo"
import { getPageBySlug } from "@/lib/wordpress-data-loader"
import { cleanWordPressContent } from "@/lib/clean-wordPress-content"
import { Breadcrumbs } from "@/components/breadcrumbs"
import type { Metadata } from "next"

const WORDPRESS_SLUG = "abandoned-vending-machines"

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 || "Abandoned Vending Machines",
    description: page.seoDescription || page.excerpt || "",
    excerpt: page.excerpt,
    date: page.date,
    modified: page.modified,
    image: page.images?.[0]?.localPath,
    path: "/blog/abandoned-vending-machines",
  })
}

export default async function AbandonedVendingMachinesPage() {
  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 || "Abandoned Vending Machines",
        description: page.seoDescription || page.excerpt || "",
        url: buildAbsoluteUrl("/blog/abandoned-vending-machines"),
        datePublished: page.date,
        dateModified: page.modified || page.date,
        type: "WebPage",
      })
    } catch (e) {
      structuredData = {
        "@context": "https://schema.org",
        "@type": "WebPage",
        headline: page.title || "Abandoned Vending Machines",
        description: page.seoDescription || "",
        url: buildAbsoluteUrl("/blog/abandoned-vending-machines"),
      }
    }

    return (
      <>
        <script
          type="application/ld+json"
          dangerouslySetInnerHTML={{ __html: JSON.stringify(structuredData) }}
        />
        <Breadcrumbs
          items={[
            { label: "Blog", href: "/blog" },
            {
              label: page.title || "Abandoned Vending Machines",
              href: "/blog/abandoned-vending-machines",
            },
          ]}
        />
        <article className="container mx-auto px-4 py-8 md:py-12 max-w-4xl">
          {content}
        </article>
      </>
    )
  } catch (error) {
    if (process.env.NODE_ENV === "development") {
      console.error("Error rendering Abandoned Vending Machines page:", error)
    }
    notFound()
  }
}
