import type { Metadata } from "next";
import { getHomePage } from "@/services/page.service";
import { getPreviewOrFallback } from "@/services/preview.service";
import { BlockRenderer } from "@/blocks/BlockRenderer";
import type { HomePageDetail } from "@/types/wagtail";

export const metadata: Metadata = {
  title: "首页",
};

export default async function Home() {
  const homePage = await getPreviewOrFallback<HomePageDetail>(
    "home.homepage",
    "body,seo_title_override,seo_description_override",
    () => getHomePage().catch(() => null),
  );

  if (!homePage) {
    return (
      <div className="mx-auto max-w-3xl px-6 py-32 text-center">
        <h1 className="mb-4 text-3xl font-semibold text-slate-900">
          尚未连接到 Wagtail CMS
        </h1>
        <p className="text-slate-600">
          请确认后端服务已启动，并在 <code>.env.local</code> 中配置了{" "}
          <code>NEXT_PUBLIC_API_URL</code>（默认 http://localhost:8000）。
        </p>
      </div>
    );
  }

  return <BlockRenderer blocks={homePage.body} />;
}
