// app/search/page.tsx
import SearchResult from '@/features/search/ui/Search';
import type { Metadata } from 'next';
import { Suspense } from 'react';

interface PageProps {
  searchParams: {
    q: string;
  };
  params: {
    locale: 'uz' | 'ru';
  };
}

export async function generateMetadata({
  searchParams,
  params,
}: PageProps): Promise<Metadata> {
  const query = (await searchParams?.q?.trim()) || '';
  const { locale } = await params;

  if (locale === 'ru') {
    const title = query
      ? `Результаты поиска по запросу “${query}” - Gastro Market`
      : 'Поиск товаров - Gastro Market';
    const description = query
      ? `Найдите все товары на Gastro Market по запросу “${query}”.`
      : 'Поиск всех товаров на Gastro Market.';

    return {
      title,
      description,
      keywords: query
        ? `поиск, ${query}, товары, gastro market`
        : 'поиск, товары, gastro market',
      openGraph: {
        title,
        description,
        url: `/search${query ? `?search=${encodeURIComponent(query)}` : ''}`,
        type: 'website',
      },
      twitter: {
        card: 'summary_large_image',
        title,
        description,
      },
    };
  }

  // Default: Uzbek
  const title = query
    ? `“${query}” bo‘yicha qidiruv natijalari - Gastro Market`
    : 'Mahsulot qidirish - Gastro Market';
  const description = query
    ? `Gastro Marketda “${query}” bo‘yicha barcha mahsulotlarni toping.`
    : 'Gastro Marketdagi barcha mahsulotlarni qidirish.';

  return {
    title,
    description,
    keywords: query
      ? `qidiruv, ${query}, mahsulotlar, gastro market`
      : 'qidiruv, mahsulotlar, gastro market',
    openGraph: {
      title,
      description,
      url: `/search${query ? `?search=${encodeURIComponent(query)}` : ''}`,
      type: 'website',
    },
    twitter: {
      card: 'summary_large_image',
      title,
      description,
    },
  };
}

const Page = () => {
  return (
    <Suspense>
      <SearchResult />
    </Suspense>
  );
};

export default Page;
