import type { Metadata } from "next";

import { HeroSection } from "@/components/home/HeroSection";
import { StatStrip } from "@/components/home/StatStrip";
import { ProductShowcase } from "@/components/home/ProductShowcase";
import { LiveFeedsSection } from "@/components/home/LiveFeedsSection";
import { TrustSection } from "@/components/home/TrustSection";
import { PartnersStrip } from "@/components/home/PartnersStrip";
import { MembershipCtaSection } from "@/components/home/MembershipCtaSection";

import {
  getCachedStudiesWithMeta,
  type StudiesCacheEntry,
} from "@/lib/studies/cache";
import {
  getCachedNewsWithMeta,
  type NewsCacheEntry,
} from "@/lib/news/cache";
import { siteConfig } from "@/lib/site-config";
import { getViewer } from "@/lib/auth/viewer";

export const metadata: Metadata = {
  title: {
    absolute: "OrthoGlobe — International Orthopaedic Charity Collaboration",
  },
  description: siteConfig.description,
};

const EMPTY_STUDIES: StudiesCacheEntry = { items: [], fetchedAt: "" };
const EMPTY_NEWS: NewsCacheEntry = { items: [], fetchedAt: "" };

export default async function HomePage() {
  // Fetch both live feeds in parallel. If either source throws (PubMed down,
  // every RSS feed failing, etc.) fall back to an empty entry so the rest of
  // the homepage still renders.
  const [studies, news, viewer] = await Promise.all([
    getCachedStudiesWithMeta().catch(() => EMPTY_STUDIES),
    getCachedNewsWithMeta().catch(() => EMPTY_NEWS),
    getViewer(),
  ]);

  return (
    <>
      <HeroSection signedIn={viewer.signedIn} />
      <StatStrip />
      <ProductShowcase />
      <LiveFeedsSection studies={studies} news={news} />
      <TrustSection />
      <PartnersStrip />
      <MembershipCtaSection />
    </>
  );
}
