import { notFound } from "next/navigation"
import { loadImageMapping } from "@/lib/wordpress-content"
import { generateRegistryMetadata, generateRegistryStructuredData } 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 = "healthy-vending"

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

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

  return generateRegistryMetadata("healthyOptions", {
    date: page.date,
    modified: page.modified,
    image: page.images?.[0]?.localPath,
  })
}

export default async function HealthyOptionsPage() {
  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>
    )

    const structuredData = generateRegistryStructuredData("healthyOptions", {
      datePublished: page.date,
      dateModified: page.modified || page.date,
    })

    return (
      <>
        <script
          type="application/ld+json"
          dangerouslySetInnerHTML={{ __html: JSON.stringify(structuredData) }}
        />
        <DropdownPageShell
          breadcrumbs={[
            { label: "Food & Beverage" },
            { label: "Healthy Options" },
          ]}
          eyebrow="Food & Beverage"
          title={page.title || "Healthy Vending Options"}
          description={
            page.seoDescription ||
            page.excerpt ||
            "Explore healthier snack and beverage options, how we balance product variety, and what a better-for-you vending mix can look like at your location."
          }
          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: "Next Step",
            title: "Need help planning the right mix?",
            description:
              "We can help you balance healthier products, traditional favorites, and machine type so the lineup works for your traffic instead of just looking good on paper.",
            actions: [
              { label: "Talk to Our Team", href: "/contact-us#contact-form" },
              {
                label: "See If You Qualify",
                href: "/#request-machine",
                variant: "outline",
              },
            ],
          }}
        />
      </>
    )
  } catch (error) {
    if (process.env.NODE_ENV === "development") {
      console.error("Error rendering Healthy Options page:", error)
    }
    notFound()
  }
}
