import type { Metadata } from 'next'
import type { Venue } from '@/lib/api/entities'
import { buildShareMetadata } from '@/lib/metadata'
import { fetchSeoRecord, seoDescription } from '@/lib/seo-data'
import { getStaticSiteSettings } from '@/lib/site-settings'

type VenueLayoutProps = {
  children: React.ReactNode
  params: Promise<{ locale: string; slug: string }>
}

export async function generateMetadata({ params }: VenueLayoutProps): Promise<Metadata> {
  const { locale, slug } = await params
  const site = getStaticSiteSettings()
    const venue = await fetchSeoRecord<Venue>('venues', slug, locale)

    if (!venue) {
        return { title: 'Training venue not found', robots: { index: false, follow: false } }
    }

    const place = [venue.city, venue.country].filter(Boolean).join(', ') || venue.city
    return buildShareMetadata({
        title: `${place} Training Venue`,
        description: seoDescription(
            venue.description,
            `Explore ${site.shortName} training facilities and upcoming courses in ${place}.`,
        ),
        path: `/information/course-venue/${venue.slug}`,
        image: venue.imageUrl,
        imageAlt: `${place} training venue`,
        site,
        locale,
    })
}

export default function VenueDetailLayout({ children }: VenueLayoutProps) {
  return children
}
