import Header from "@/widgets/home/Header/Header";
import StatisticsSection from "@/widgets/home/StatisticsSection/StatisticsSection";
import RatingSection from "@/widgets/home/RatingSection/RatingSection";
import NewsSection from "@/widgets/home/NewsSection/NewsSection";
import MapSection from "@/widgets/home/MapSection/MapSection";
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 | Сделаем дороги безопасными!",
      openGraph: {
        title: "KG ROAD | Главная",
        description:
          "Главная страница KG ROAD | Сделаем дороги безопасными!",
        type: "website",
      },
    };

  const metadata = data.filter((tag) => tag.page === "home")[0];

  if (!metadata) {
    return {
      title: "KG ROAD | Главная",
      description:
        "Главная страница KG ROAD | Сделаем дороги безопасными!",
      openGraph: {
        title: "KG ROAD | Главная",
        description:
          "Главная страница KG ROAD | Сделаем дороги безопасными!",
        type: "website",
      },
    };
  }

  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 Home = async ({
  searchParams,
}: {
  searchParams: {
    ["тип-дороги"]: string;
    ["поиск-на-карте"]: string;
    ["поиск-рейтинг"]: string;
    ["страница-рейтинга"]: string;
  };
}) => {
  return (
    <div className="home">
      <Header />
      <StatisticsSection />
      <MapSection searchParams={searchParams} />
      <RatingSection searchParams={searchParams} />
      <NewsSection />
    </div>
  );
};

export default Home;
