import fs from "fs/promises";
import path from "path";
import { auth } from "../../auth";
import { HomeClient } from "./HomeClient";

export type HomeContent = {
  hero: { title: string; subtitle: string; ctaPrimary: string; ctaSecondary: string };
  features: { icon: string; title: string; desc: string; href: string }[];
  cta: { title: string; subtitle: string; button: string };
};

export default async function HomePage() {
  const raw = await fs.readFile(
    path.join(process.cwd(), "content", "pages", "home.json"),
    "utf-8"
  );
  const content: HomeContent = JSON.parse(raw);
  const session = await auth();
  const isAdmin = (session?.user as { role?: string })?.role === "admin";

  const sliderDir = path.join(process.cwd(), "public", "images", "slider");
  const sliderFiles = await fs.readdir(sliderDir).catch(() => []);
  const sliderImages = sliderFiles
    .filter((f) => /\.(jpe?g|png|webp|gif)$/i.test(f))
    .sort()
    .map((f) => `/images/slider/${f}`);

  return <HomeClient initialContent={content} isAdmin={isAdmin} sliderImages={sliderImages} />;
}
