import { getLocale, getTranslations } from 'next-intl/server'
import { getProductCatalog } from '@/lib/db/queries/getProductCatalog'
import SectionHeading from './SectionHeading'
import ProductTabsClient, { type ProductTab } from './ProductTabsClient'

const PER_TAB = 8

// "Modern" homepage: New arrivals / Featured / Top rated, all from the real
// catalog query (cached, tag-invalidated). A tab with no products is dropped;
// the whole section hides when none have any.
export default async function ProductTabs() {
  const locale = await getLocale()
  const t = await getTranslations('HomeDesigns')

  const [newest, featured, topRated] = await Promise.all([
    getProductCatalog(locale, { sort: 'newest', page: 1, perPage: PER_TAB }),
    getProductCatalog(locale, { featuredOnly: true, sort: 'newest', page: 1, perPage: PER_TAB }),
    getProductCatalog(locale, { sort: 'rating', minRating: 1, page: 1, perPage: PER_TAB }),
  ])

  const tabs: ProductTab[] = [
    { key: 'new', label: t('products.tabNew'), products: newest.products },
    { key: 'featured', label: t('products.tabFeatured'), products: featured.products },
    { key: 'top', label: t('products.tabTopRated'), products: topRated.products },
  ].filter((tab) => tab.products.length > 0)

  if (tabs.length === 0) return null

  return (
    <section className="home-reveal">
      <SectionHeading
        eyebrow={t('products.eyebrow')}
        title={t('products.title')}
        action={{ href: '/products', label: t('products.viewAll') }}
      />
      <ProductTabsClient tabs={tabs} ariaLabel={t('products.title')} />
    </section>
  )
}
