import { HomePageClient } from '@/components/HomePageClient'
import { BlogPost } from '@/types/blog'

async function getBlogPosts(): Promise<BlogPost[]> {
  const baseUrl = process.env.API_BASE_URL || process.env.NEXT_PUBLIC_API_BASE_URL || 'http://localhost:4005'

  try {
    const response = await fetch(`${baseUrl}/posts`, {
      cache: 'no-store',
      next: { revalidate: 0 }
    })

    if (!response.ok) {
      console.error(`[frontend] Failed to load posts: ${response.status}`)
      return []
    }

    const payload = await response.json()
    return Array.isArray(payload.data) ? payload.data : []
  } catch (error) {
    console.error('[frontend] Failed to load posts', error)
    return []
  }
}

export default async function HomePage() {
  const posts = await getBlogPosts()
  return <HomePageClient posts={posts} />
}
