import { prisma } from "@/lib/prisma";

export async function getBackgroundImage(
  page: "home" | "events" | "leaderboard",
  defaultImage: string
): Promise<string> {
  try {
    const sitePreferences = await prisma.sitePreferences.findUnique({
      where: { id: "global" },
    });

    if (!sitePreferences) {
      return defaultImage;
    }

    const imageKey = `${page}Background` as keyof typeof sitePreferences;
    const customImage = sitePreferences[imageKey];

    return (customImage as string | null) || defaultImage;
  } catch (error) {
    console.error("Error fetching background image:", error);
    return defaultImage;
  }
}

