import { notFound } from "next/navigation"
import type { Metadata } from "next"
import { getAllLocationSlugs, getLocationBySlug } from "@/lib/location-data"
import {
  generateLocationPageMetadata,
  LocationLandingPage,
} from "@/components/location-landing-page"

interface LocationPageProps {
  params: Promise<{ location: string }>
}

const NON_LOCATION_ROUTES = ["machines-we-use", "machines-for-sale"]

export const dynamic = "force-static"
export const dynamicParams = false

export async function generateStaticParams() {
  const slugs = getAllLocationSlugs()

  if (!slugs.length) {
    return [{ location: "ogden-utah" }]
  }

  return slugs.map((slug) => ({ location: slug }))
}

export async function generateMetadata({
  params,
}: LocationPageProps): Promise<Metadata> {
  const { location } = await params

  if (NON_LOCATION_ROUTES.includes(location)) {
    return { title: "Page Not Found | Rocky Mountain Vending" }
  }

  const locationData = getLocationBySlug(location)

  if (!locationData) {
    return { title: "Location Not Found | Rocky Mountain Vending" }
  }

  return generateLocationPageMetadata(locationData)
}

export default async function LocationPage({ params }: LocationPageProps) {
  const { location } = await params

  if (NON_LOCATION_ROUTES.includes(location)) {
    notFound()
  }

  const locationData = getLocationBySlug(location)

  if (!locationData) {
    notFound()
  }

  return <LocationLandingPage locationData={locationData} />
}
