import BreadCrumbs from "@/features/BreadCrumbs/BreadCrumbs";
import "./News.scss";
import Typography from "@/shared/ui/components/Typography/Typography";
import NewsList from "@/widgets/NewsList/NewsList";
import { Metadata } from "next";
import { apiInstance } from "@/shared/config/apiConfig";
import { IMetatag } from "@/shared/types/metatag-type";

export async function generateMetadata(): Promise<Metadata> {
  const data = await apiInstance
    .get<IMetatag[]>("/metatags/")
    .then((res) => res.data)
    .catch((e) => console.log(e));

  if (!data)
    return {
      title: "KG ROAD | Новости",
      description: "Страница новостей KG ROAD",
    };

  const metadata = data.filter((tag) => tag.page === "news")[0];
  if (!metadata) {
    return {
      title: "KG ROAD | Новости",
      description: "Страница новостей KG ROAD",
    };
  }

  return {
    title: `KG ROAD | ${metadata.title}`,
    description: metadata.description,
    keywords: metadata.keywords.split(","),
    openGraph: {
      title: `KG ROAD | ${metadata.title}`,
      description: metadata.description,
      type: "website",
    },
  };
}

const News = ({
  searchParams,
}: {
  searchParams: {
    ["страница-новостей"]: string;
  };
}) => {
  return (
    <div className="news page-padding">
      <BreadCrumbs homeRequired />

      <Typography element="h2">Новости</Typography>

      <NewsList searchParams={searchParams} />
    </div>
  );
};

export default News;
