// Force dynamic rendering - prevent Next.js from caching server component data
export const dynamic = 'force-dynamic';
export const revalidate = 0;

import React, { Suspense } from "react";
import { unstable_noStore as noStore } from 'next/cache';
import { cookies } from 'next/headers';
import { storyService } from "@/services/storyService";
import { productService } from "@/services/productService";
import { companyService } from "@/services/companyService";
import Loading from "@/components/ui/loading";
import { parseMetaTags } from "@/utils/seoUtils";
import { commonService } from "@/services/commonService";
import { arrangeItemsWithAds } from "@/utils/helpersUtils";
import CompanySection from "@/components/ui/company/CompanySection";
import HomePage from "@/components/pages/HomePage";
import ProductVipSection from "@/components/ui/product/ProductVipSection";
import { ArrayUtils } from "@/utils/arrayUtils";

export async function generateMetadata({ params }) {
    const res = await commonService.fetchMeta();
    const meta = res.arrMeta;
    return parseMetaTags(meta)
} 

function LoadingFallback() {
    return (
        <Loading loading={true} fullScreen={true} />
    )
}

async function HomePageContent({ locale }) {
    // CRITICAL: Opt out of caching for this function
    noStore();

    console.log('📄 HomePageContent: Fetching data with locale:', locale);

    const [
        storyResponse,
        vipResponse,
        blockItems,
        lastResponse,
        storeResponse,
        actionResponse,
        categorySeoResponse,
    ] = await Promise.all([
        storyService.fetchStories({ locale }),
        productService.fetchProductVIP({ locale }),
        productService.fetchProductBlock({ locale }),
        productService.fetchProductLast({ locale }),
        companyService.fetchCompanies({ locale }),
        productService.fetchActions({ locale }),
        commonService.fetchCategorySeoById({ locale }),
    ]);

    console.log('✅ HomePageContent: Data fetched successfully for locale:', locale);


    const arrangedProducts = arrangeItemsWithAds(lastResponse?.products, 
        [
            {
                type: 'company_section',
                content: <CompanySection sectionClassName="col-span-12 product-ads-section" items={storeResponse?.companies} showFirstItem={false} actionBoxShow={false} />
            },
        ],
        [
            {
                type: 'vip_section',
                content: <ProductVipSection sectionClassName="col-span-12 product-ads-section" items={ArrayUtils.from(vipResponse?.products).shuffle().slice(1, 4).getValue()} showFirstItem={false} actionBoxShow={false} />
            }
        ]
    );

    return <HomePage
        key={locale}
        storyResponse={storyResponse}
        vipResponse={vipResponse}
        blockItems={blockItems}
        lastResponse={lastResponse}
        storeResponse={storeResponse}
        actionResponse={actionResponse}
        arrangedProducts={arrangedProducts}
        categorySeoResponse={categorySeoResponse}
    />
}


export default async function Home({ params: { locale } }) {
    // ✅ CRITICAL: Access cookies to bust Next.js cache
    const cookieStore = await cookies();
    cookieStore.get('NEXT_LOCALE'); // Force dynamic behavior

    return (
        <>
            <Suspense fallback={<LoadingFallback />}>
                <HomePageContent locale={locale} />
            </Suspense>
        </>

    )
}