import SubCategory from '@/features/category/ui/SubCategory';
import { category_api } from '@/shared/config/api/category/api';
import { BASE_URL } from '@/shared/config/api/URLs';
import { Metadata } from 'next';
import { Suspense } from 'react';

interface Params {
  params: {
    categoryId: string;
  };
}

const fetchCategory = async (categoryId: string) => {
  try {
    const res = await category_api.getCategory({ page: 1, page_size: 99 });
    return res.data.find((c) => c.id.toString() === categoryId);
  } catch {
    return null;
  }
};

export async function generateMetadata({ params }: Params): Promise<Metadata> {
  const { categoryId } = await params;
  const category = await fetchCategory(categoryId);

  if (!category) {
    return {
      title: 'Kategoriya topilmadi',
      description: 'Kategoriya mavjud emas',
    };
  }

  return {
    title: `${category.name} | GASTRO`,
    description: `Bizning do‘konimizdagi ${category.name} kategoriyasidagi mahsulotlar`,
    keywords: `mahsulotlar, kategoriya, ${category.name}`,
    openGraph: {
      title: `${category.name} | GASTRO`,
      description: `Bizning do‘konimizdagi ${category.name} kategoriyasidagi mahsulotlar`,
      siteName: 'GASTRO',
      images: [
        {
          url: category.image ? BASE_URL + category.image : '/logos/logo.png',
          width: 1200,
          height: 1200,
          alt: category.name,
        },
      ],
      type: 'website',
    },
    twitter: {
      card: 'summary_large_image',
      title: `${category.name} | GASTRO`,
      description: `Bizning do‘konimizdagi ${category.name} kategoriyasidagi mahsulotlar`,
      images: [category.image ? BASE_URL + category.image : '/logos/logo.png'],
    },
  };
}

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

export default Page;
