import Banner from "@/components/banner/Banner";
import Category from "@/components/sidebar/Category";
import { getSiteSettings } from "@/utils/data";
import { notFound } from 'next/navigation';
import DynamicListPage from "@/components/common/DynamicListPage";
import { Suspense } from 'react';
import { getPageData } from "@/utils/data";
import { getLocalPageData } from "@/data/presentation";
import Presentation from "@/components/presentation/Presentation";

const baseSlug = 'presentation';

const LoadingSpinner = () => (
  <div className="flex justify-center items-center p-8">
    <div className="animate-spin rounded-full h-12 w-12 border-b-2 border-blue-500"></div>
  </div>
);

export const revalidate = 3600;

export async function generateMetadata({ params }) {
  const resolvedParams = await params;
  const slug = resolvedParams.slug || [];
  const slugArr = [baseSlug, ...(Array.isArray(slug) ? slug : [slug])];
  const { data, error, page_info } = await getPageData({ slug_list: slugArr });
  const siteSettings = await getSiteSettings();
  const siteTitle = siteSettings.site_title || '';
  const siteName = siteSettings.site_name || '';
  const siteNameInPageTitles = siteSettings.site_name_in_page_titles || 'None';

  let title = '';
  if (error) {
    title = error.title || 'Page Error';
    return {
      title,
      description: error.message || '',
    };
  }
  if (Array.isArray(data) && page_info) {
    title = page_info.meta_title || page_info.title || '';
    if (siteName && title) {
      if (siteNameInPageTitles === 'After') {
        title = `${title} - ${siteName}`;
      } else if (siteNameInPageTitles === 'Before') {
        title = `${siteName} - ${title}`;
      }
    }
    return {
      title,
      description: page_info.meta_description || '',
    };
  }
  title = data?.meta_title || data?.title || '';
  if (siteName && title) {
    if (siteNameInPageTitles === 'After') {
      title = `${title} - ${siteName}`;
    } else if (siteNameInPageTitles === 'Before') {
      title = `${siteName} - ${title}`;
    }
  }
  return {
    title,
    description: data?.meta_description || '',
  };
}

export default async function Page({ params, searchParams }) {
  const resolvedParams = await params;
  const slug = resolvedParams.slug || [];
  const slugArr = [baseSlug, ...(Array.isArray(slug) ? slug : [slug])];

  const siteSettings = await getSiteSettings();
  const pageSize = Number(siteSettings.page_size) || 12;

  const { data, error, total } = await getPageData({
    slug_list: slugArr,
    page: 1,
    page_size: pageSize,
    downloadFiles: true,
  });

  if (error) {
    notFound();
  }
  const bannerComponentName = 'Banner-' + baseSlug;
  const categoryComponentName = 'Category-' + baseSlug;
  if (Array.isArray(data)) {
    const currentPath = '/' + slugArr.join('/');
    const listColumns = 4; 
    return (
      <>
        <Banner componentName={bannerComponentName} />
        <div className="wrapper !bg-[#ffffff]">
          <div className="container py-[4.5rem] xl:!py-24 lg:!py-24 md:!py-24">
            <div className="flex flex-col md:flex-row mx-[-15px] xl:mx-[-35px] lg:mx-[-20px]">
              <Category componentName={categoryComponentName} />
              <div className="flex-1 min-w-0 !px-[15px] max-w-full md:!px-[20px] lg:!px-[20px] xl:!px-[35px]">
                <Suspense fallback={<LoadingSpinner />}>
                  <DynamicListPage
                    initialItems={data}
                    slugArr={slugArr}
                    basePath={currentPath}
                    columns={listColumns}
                    pageSize={pageSize}
                    totalItems={total}
                    searchParams={searchParams}
                  />
                </Suspense>
              </div>
            </div>
          </div>
        </div>
      </>
    );
  } else {
    notFound();
  }
} 
