// import 'swiper/css';
// import 'swiper/css/navigation';
// import Head from 'next/head';
// import FifePostSection from '../components/HomeSection/FifePostSection/FifePostSection';
// import FourPostSection from '../components/HomeSection/fourPostSection/FourPostSection';
// import { useMagazinePosts, MagazinePostsState } from '../utils/useMagazinePosts';
// import SixPostSection from '../components/HomeSection/SixPostSection/SixPostSection';
// import { useRouter } from 'next/router';
// import { GetStaticPropsContext } from 'next';
// import { useEffect, useState } from 'react';
// import MagazineApis from '../utils/MagazineApis';
// import { Category, LaningPage } from '../types';
// import { serverSideTranslations } from 'next-i18next/serverSideTranslations';

// const Index = () => {
//     const router = useRouter();
//     const { locale } = router;
//     // console.log("locale",locale); 
//     const latestNewsPosts: MagazinePostsState = useMagazinePosts(11, locale);

//     const [LandingPageSectionOne, setLandingPageSectionOne] = useState<LaningPage[]>([]);
//     const [LandingPageSectionTwo, setLandingPageSectionTwo] = useState<LaningPage[]>([]);
//     const [LandingPageSectionThree, setLandingPageSectionThree] = useState<LaningPage[]>([]);

//     useEffect(() => {
//         getLandingPage(locale);
//     }, [locale]);

//     const getLandingPage = (locale: any) => {
//         MagazineApis.getLandingPage(locale).then((res) => {
//             setLandingPageSectionOne(res.data[0].section_1[0]);
//             setLandingPageSectionTwo(res.data[0].section_2[0]);
//             setLandingPageSectionThree(res.data[0].section_3[0]);
//             // console.log('loggggg', res.data[0]);
//         });
//     };

//     return (
//         <div>
//             <Head>
//                 <title>مجلة الاستثمار - أحدث المقالات والأخبار عن الاستثمار والتمويل</title>
//             </Head>

//             <FifePostSection fifePosts={LandingPageSectionOne} />

//             <FourPostSection FourPosts={LandingPageSectionTwo} latestNewsPost={latestNewsPosts.posts[0]} />

//             <SixPostSection SixPosts={LandingPageSectionThree} />
//         </div>
//     );
// };

// export default Index;

// export async function getStaticProps(context: GetStaticPropsContext) {
//     return {
//         props: {
//             ...(await serverSideTranslations(context.locale || 'ar', ['common'])),
//         },
//     };
// }

import 'swiper/css';
import 'swiper/css/navigation';
import Head from 'next/head';
import FifePostSection from '../components/HomeSection/FifePostSection/FifePostSection';
import FourPostSection from '../components/HomeSection/fourPostSection/FourPostSection';
import SixPostSection from '../components/HomeSection/SixPostSection/SixPostSection';
import { MagazinePostsState, useMagazinePosts } from '../utils/useMagazinePosts';
import { GetStaticProps, GetStaticPropsContext } from 'next';
import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
import MagazineApis from '../utils/MagazineApis';
import { LaningPage } from '../types';

interface IndexProps {
  landingPageSectionOne: LaningPage;
  landingPageSectionTwo: LaningPage;
  landingPageSectionThree: LaningPage;
  locale: string;
}

const Index = ({ landingPageSectionOne, landingPageSectionTwo, landingPageSectionThree, locale }: IndexProps) => {
  // Fetch the latest news posts
  
  const latestNewsPosts: MagazinePostsState = useMagazinePosts(11, locale);

  return (
    <div>
      <Head>
        <title>مجلة الاستثمار - أحدث المقالات والأخبار عن الاستثمار والتمويل</title>
      </Head>

      <FifePostSection fifePosts={landingPageSectionOne} />
      <FourPostSection FourPosts={landingPageSectionTwo} latestNewsPost={latestNewsPosts.posts[0]} />
      <SixPostSection SixPosts={landingPageSectionThree} />
    </div>
  );
};

export default Index;

export const getStaticProps: GetStaticProps = async (context: GetStaticPropsContext) => {
  const locale = context.locale || 'ar';

  // Fetch landing page data during build time
  const res = await MagazineApis.getLandingPage(locale);
  const landingPageSectionOne = res.data[0].section_1[0] || {};
  const landingPageSectionTwo = res.data[0].section_2[0] || {};
  const landingPageSectionThree = res.data[0].section_3[0] || {};

  return {
    props: {
      landingPageSectionOne,
      landingPageSectionTwo,
      landingPageSectionThree,
      locale,
      ...(await serverSideTranslations(locale, ['common'])),
    },
    revalidate: 60, // Optional: revalidate every 60 seconds
  };
};
