import Breadcrumb from "@/app/components/Breadcrumb";
import NewsSection from "./components/NewsSection";
import { fetchBlogList } from "@/api/blogsApi";

interface NewsPageProps {
    searchParams?: Promise<{ search?: string; page?: string }> | { search?: string; page?: string };
}

export default async function NewsPage({ searchParams }: NewsPageProps) {
    const resolvedSearchParams = searchParams instanceof Promise ? await searchParams : searchParams;
    const searchQuery = resolvedSearchParams?.search?.toString() || "";
    const pageParam = resolvedSearchParams?.page?.toString() || "1";
    const currentPage = Number.parseInt(pageParam, 10) || 1;

    // Fetch blog list from API
    const blogResponse = await fetchBlogList({
        page: currentPage,
        limit: 3,
        ...(searchQuery ? { search: searchQuery } : {}),
    });
    const { blogs, pagination } = blogResponse.data;

    return (
        <>
            <Breadcrumb title={searchQuery ? `Search: ${searchQuery}` : "Blog Standard"} current="Blog Standard" />
            <NewsSection blogs={blogs} searchQuery={searchQuery} pagination={pagination} />
        </>
    );
}
