import axios from "@/lib/axios";
import BreadcrumbCustom from "@/components/constantElements/BreadcrumbCustom";
import PackageItem from "@/components/packages/PackageItem";
import { notFound, redirect } from "next/navigation";

const frontUrl = process.env.NEXT_PUBLIC_FRONTEND_URL;
const backendUrlImage = process.env.NEXT_PUBLIC_BACKEND_URL_IMAGE;
const backendUrl = process.env.NEXT_PUBLIC_BACKEND_URL;

export async function generateMetadata({ params, searchParams }) {
  const newSearchParams = new URLSearchParams(searchParams).toString();
  let video = false;
  const slug = params.handler.join("/");
  const pageData = await axios
    .get(
      searchParams
        ? `api/packages/${slug}?${newSearchParams}`
        : `api/packages/${slug}`
    )
    .then((result) => {
      return result;
    })
    .catch((error) => {
      if (error.response.status == "301" || error.response.status == "302") {
        if (
          error.response.data.type == "error" &&
          error.response.data.message == "redirect" &&
          error.response.data.redirect
        ) {
          redirect(error.response.data.redirect);
        } else {
          redirect(notFound());
        }
      } else {
        redirect(notFound());
      }
    });

    

    const next =
    pageData.data.package.products&&
    pageData.data.package.products.next_page_url &&
    pageData.data.package.products.next_page_url.replace(
      `${backendUrl}/api/`,
      frontUrl
    );
  const prev =
  pageData.data.package.products&&
  pageData.data.package.products.prev_page_url &&
  pageData.data.package.products.prev_page_url.replace(
      `${backendUrl}/api/`,
      frontUrl
    );

  return {
    title: pageData.data.package.title,
    description: pageData.data.package.description,
    keywords: pageData.data.package.keywords,
    openGraph: {
      title: pageData.data.package.title,
      description: pageData.data.package.description,
      keywords: pageData.data.package.keywords,
    },
    alternates: {
      canonical: `${frontUrl}packages/${pageData.data.package.slug}`,
    },

    robots: {
      index: pageData.data.package.index_status == 0 ? false : true,
      follow: pageData.data.package.index_status == 0 ? false : true,
    },
    ...(video
      ? pageData.data.package.products
        ? pageData.data.package.products.next_page_url &&
          pageData.data.package.products.prev_page_url
          ? {
              icons: {
                other: [
                  {
                    rel: "next",
                    url: `${next}`,
                  },
                  {
                    rel: "prev",
                    url: `${prev}`,
                  },
                  {
                    rel: "preconnect",
                    url: "https://upload.vidbeen.ir",
                  },
                  {
                    rel: "dns-prefetch",
                    url: "https://upload.vidbeen.ir",
                  },
                  {
                    rel: "preconnect",
                    url: backendUrl,
                  },
                ],
              },
            }
          : pageData.data.package.products.prev_page_url &&
            !pageData.data.package.products.next_page_url
          ? {
              icons: {
                other: [
                  {
                    rel: "prev",
                    url: `${prev}`,
                  },
                  {
                    rel: "preconnect",
                    url: "https://upload.vidbeen.ir",
                  },
                  {
                    rel: "dns-prefetch",
                    url: "https://upload.vidbeen.ir",
                  },
                  {
                    rel: "preconnect",
                    url: backendUrl,
                  },
                ],
              },
            }
          : pageData.data.package.products.next_page_url &&
            !pageData.data.package.products.prev_page_url
          ? {
              icons: {
                other: [
                  {
                    rel: "next",
                    url: `${next}`,
                  },
                  {
                    rel: "preconnect",
                    url: "https://upload.vidbeen.ir",
                  },
                  {
                    rel: "dns-prefetch",
                    url: "https://upload.vidbeen.ir",
                  },
                  {
                    rel: "preconnect",
                    url: backendUrl,
                  },
                ],
              },
            }
          : {
              icons: {
                other: [
                  {
                    rel: "preconnect",
                    url: "https://upload.vidbeen.ir",
                  },
                  {
                    rel: "dns-prefetch",
                    url: "https://upload.vidbeen.ir",
                  },
                  {
                    rel: "preconnect",
                    url: backendUrl,
                  },
                ],
              },
            }
        : {
            icons: {
              other: [
                {
                  rel: "preconnect",
                  url: "https://upload.vidbeen.ir",
                },
                {
                  rel: "dns-prefetch",
                  url: "https://upload.vidbeen.ir",
                },
                {
                  rel: "preconnect",
                  url: backendUrl,
                },
              ],
            },
          }
      : pageData.data.package.products
      ? pageData.data.package.products.next_page_url &&
        pageData.data.package.products.prev_page_url
        ? {
            icons: {
              other: [
                {
                  rel: "next",
                  url: `${next}`,
                },
                {
                  rel: "prev",
                  url: `${prev}`,
                },
                {
                  rel: "preconnect",
                  url: backendUrl,
                },
              ],
            },
          }
        : pageData.data.package.products.prev_page_url &&
          !pageData.data.package.products.next_page_url
        ? {
            icons: {
              other: [
                {
                  rel: "prev",
                  url: `${prev}`,
                },
                {
                  rel: "preconnect",
                  url: backendUrl,
                },
              ],
            },
          }
        : pageData.data.package.products.next_page_url &&
          !pageData.data.package.products.prev_page_url
        ? {
            icons: {
              other: [
                {
                  rel: "next",
                  url: `${next}`,
                },
                {
                  rel: "preconnect",
                  url: backendUrl,
                },
              ],
            },
          }
        : {
            icons: {
              other: [
                {
                  rel: "preconnect",
                  url: backendUrl,
                },
              ],
            },
          }
      : null),
  };
}

export default async function Page({ params, searchParams }) {

   
  let values = Object.values(searchParams)
  values.splice(values.length - 1 , 1)
  const newSearchParams = new URLSearchParams(searchParams).toString();
  const slug = params.handler.join("/");
  const pageData = await axios.get(
    searchParams
      ? `api/packages/${slug}?${newSearchParams}`
      : `api/packages/${slug}`
  );


  return (
    <>
      <BreadcrumbCustom
        categorySlug={pageData.data.package.bread_crumb.slug}
        breadcrumb={pageData.data.package.bread_crumb}
        parentId={pageData.data.package.parent_id}
        categoryName={pageData.data.package.name}
        type="package"
        structure={pageData.data.package.json_bread_crumb}
      />
      <PackageItem
        apiData={pageData.data.package}
        pages={searchParams.page}
        scrollTo={newSearchParams}
        testValue = {values}
      />
    </>
  );
}
