import HeroSection from './components/home/HeroSection';
import QuickLinksGrid from './components/home/QuickLinksGrid'; 
import localHomeData from './home.json';
import { getCmsImageUrl } from '@/utils/image';
import { fetchHomeData } from '@/api';


// Force dynamic rendering - không cache
export const dynamic = 'force-dynamic';

export default async function Home() {
  // Fetch home data (blog aggregation is now handled by the backend)
  const apiHomeData = await fetchHomeData();
  
  // Use API home data if available, otherwise fallback to local JSON
  const data = JSON.parse(JSON.stringify(apiHomeData || localHomeData));

  // Process Hero Image URL
  if (data.hero?.backgroundImage) {
    data.hero.backgroundImage = getCmsImageUrl(data.hero.backgroundImage);
  }

  // Process blog images from CMS (thumbnail normalization)
  if (data.blogPreview?.items) {
    data.blogPreview.items = data.blogPreview.items.map((item: any) => ({
      ...item,
      thumbnail: getCmsImageUrl(item.thumbnail) || '/assets/img/home-1/news/01.jpg'
    }));
  }

  return (
    <>
      <HeroSection/>
      <QuickLinksGrid/>
    </>
  );
}
