import type { Metadata } from 'next'
import { getTranslations } from 'next-intl/server'
import CartPageClient from '@/components/frontend/cart/CartPageClient'
import { getLanguages, getDefaultLanguage } from '@/lib/db/queries/getlanguages'
import { getSiteInfo } from '@/lib/db/queries/getSiteInfo'
import { buildLocalizedPath } from '@/lib/i18n/buildLocalizedPath'

interface CartPageProps {
  params: Promise<{ locale: string }>
}

const APP_URL = (process.env.NEXT_PUBLIC_APP_URL ?? 'http://localhost:3000').replace(/\/$/, '')

// Same full metadata shape as the product page's generateMetadata (title,
// description, keywords, canonical + hreflang, OG, Twitter, robots) — but
// static: a cart has no single product to describe, so the copy comes from
// the `Cart.metaTitle`/`Cart.metaDescription` i18n keys (src/messages/*.json)
// instead of a DB row per request — editable per-language from the admin
// Languages module without a code change, same as every other translatable
// string in the app. `alternates.languages` still needs every *active*
// language and the real site name, hence this stays a generateMetadata
// function rather than a bare `export const metadata` object.
export async function generateMetadata({ params }: CartPageProps): Promise<Metadata> {
  const { locale } = await params
  const [languages, defaultLanguage, siteInfo, t] = await Promise.all([
    getLanguages(),
    getDefaultLanguage(),
    getSiteInfo(),
    getTranslations({ locale, namespace: 'Cart' }),
  ])

  const defaultLocale = defaultLanguage?.code ?? 'en'
  const canonicalPath = buildLocalizedPath(locale, defaultLocale, '/cart')

  const title = t('metaTitle')
  const description = t('metaDescription', { siteName: siteInfo.siteName })
  const ogImages = siteInfo.logoUrl ? [{ url: siteInfo.logoUrl, alt: siteInfo.logoAlt }] : undefined

  return {
    title: `${title} | ${siteInfo.siteName}`,
    description,
    keywords: ['shopping cart', 'checkout', 'online shopping', siteInfo.siteName],
    alternates: {
      canonical: `${APP_URL}${canonicalPath}`,
      languages: {
        ...Object.fromEntries(
          languages.map((lang) => [lang.code, `${APP_URL}${buildLocalizedPath(lang.code, defaultLocale, '/cart')}`]),
        ),
        'x-default': `${APP_URL}${buildLocalizedPath(defaultLocale, defaultLocale, '/cart')}`,
      },
    },
    openGraph: {
      title,
      description,
      siteName: siteInfo.siteName,
      url: `${APP_URL}${canonicalPath}`,
      images: ogImages,
      type: 'website',
    },
    twitter: {
      card: 'summary_large_image',
      title,
      description,
      images: ogImages?.map((img) => img.url),
    },
    // A shopping cart is private, per-visitor content with nothing unique to
    // rank on — `noindex` regardless of what any single visitor's cart holds,
    // same convention every major storefront applies to this route. `follow`
    // stays true — links out of the page (to products, checkout) are still
    // fine for a crawler to traverse.
    robots: { index: false, follow: true },
  }
}

export default function CartPage() {
  return <CartPageClient />
}
